Regexp: modify $1 within the replace clause?

Discussion in 'Perl' started by Yaroslav, Apr 16, 2004.

  1. Yaroslav

    Yaroslav Guest

    Hi all,

    Consider the following trivial example:

    $_ =~ s/<matching_expression>/<clause_that_uses_$1>/g;

    Is there a way to build such a regular expression, that matches some
    string and modifies the match (e.g. $1) before using it in the replace
    clause? What if I want to do something with $1 before I use it to
    replace something? Is there a way to insert, say, a function of $1
    there, rather than $1 itself?

    TIA.

    ~yaroslav
    Yaroslav, Apr 16, 2004
    #1
    1. Advertising

  2. Yaroslav

    Joe Smith Guest

    Yaroslav wrote:

    > $_ =~ s/<matching_expression>/<clause_that_uses_$1>/g;
    > Is there a way to insert, say, a function of $1
    > there, rather than $1 itself?


    Read the docs, looking for e and ee modifiers.
    s/(.*)/foo($1)/eeg;
    -Joe
    Joe Smith, Apr 16, 2004
    #2
    1. Advertising

  3. Yaroslav

    Guest

    Joe Smith <> wrote in message news:<NMWfc.5448$yD1.21555@attbi_s54>...
    > Yaroslav wrote:
    >
    > > $_ =~ s/<matching_expression>/<clause_that_uses_$1>/g;
    > > Is there a way to insert, say, a function of $1
    > > there, rather than $1 itself?

    >
    > Read the docs, looking for e and ee modifiers.
    > s/(.*)/foo($1)/eeg;


    Yes, do read the docs. The OP wants /e not /ee

    This newsgroup does not exist (see FAQ). Please do not follow-up here
    without pointing this out.
    , Apr 21, 2004
    #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. Tim Jowers
    Replies:
    1
    Views:
    720
    Tim Jowers
    Apr 15, 2004
  2. Greg Hurrell
    Replies:
    4
    Views:
    145
    James Edward Gray II
    Feb 14, 2007
  3. Jeremy Stephens

    can't modify literal regexp: why?

    Jeremy Stephens, Nov 13, 2007, in forum: Ruby
    Replies:
    4
    Views:
    95
    Eric Hodel
    Nov 14, 2007
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    445
    Mikel Lindsaar
    Mar 31, 2008
  5. Joao Silva
    Replies:
    16
    Views:
    328
    7stud --
    Aug 21, 2009
Loading...

Share This Page