Replace <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>

Discussion in 'Perl Misc' started by Udo, Sep 2, 2007.

  1. Udo

    Udo Guest

    Hello,

    I'm looking for an RegEx which replaces
    <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
    with
    <Word> <Pattern0> Literal <Word> <Pattern1> Literal ... <Word>
    <PatternN>

    Word is just a single word (not a literal)
    ,_ is a delimiter (just a ',' and optionally Spaces), not so important
    <Pattern0> ... <PatternN> are always the same patterns.
    Literal is always the same.

    Main problem is the correct grouping for the backreferences.

    Thanks and greetings
    Udo
    Udo, Sep 2, 2007
    #1
    1. Advertising

  2. Udo wrote:
    > I'm looking for an RegEx which replaces
    > <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
    > with
    > <Word> <Pattern0> Literal <Word> <Pattern1> Literal ... <Word>
    > <PatternN>


    C:\home>type test.pl
    my $string = '<Word>,_<Pattern0>,_<Pattern1>,_<Pattern2>';
    my @words = ('', 'foo', 'bar');
    $string =~ s/,_\s*(<Pattern(\d+)>)/ $words[$2] $1/g;
    print $string, "\n";

    C:\home>test.pl
    <Word> <Pattern0> foo <Pattern1> bar <Pattern2>

    C:\home>

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 2, 2007
    #2
    1. Advertising

  3. Udo

    Anno Siegel Guest

    On 2007-09-02 01:00:33 +0200, Udo <> said:

    > Hello,
    >
    > I'm looking for an RegEx which replaces


    Strictly speaking, a regex doesn't replace things. A substitution
    operator (s///),
    the first part of which is a regex, replaces things.

    > <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
    > with
    > <Word> <Pattern0> Literal <Word> <Pattern1> Literal ... <Word>
    > <PatternN>
    >
    > Word is just a single word (not a literal)


    What do you mean by "literal"?

    > ,_ is a delimiter (just a ',' and optionally Spaces), not so important
    > <Pattern0> ... <PatternN> are always the same patterns.
    > Literal is always the same.
    >
    > Main problem is the correct grouping for the backreferences.


    It seems that in effect you want to change the delimiter to " Literal "
    everywhere
    in the string.

    s/,\s*/ Literal /g;

    Is that what you want?

    Anno
    Anno Siegel, Sep 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Blais
    Replies:
    1
    Views:
    378
    Bruno Desthuilliers
    Jun 27, 2006
  2. Greg Ewing
    Replies:
    2
    Views:
    344
    Dieter Maurer
    Jun 29, 2006
  3. Alun
    Replies:
    3
    Views:
    4,511
    Masudur
    Feb 18, 2008
  4. Prasad S
    Replies:
    2
    Views:
    230
    Dr John Stockton
    Aug 27, 2004
  5. Replies:
    3
    Views:
    174
    Brian McCauley
    Sep 12, 2005
Loading...

Share This Page