Substitutions in matched patterns?

Discussion in 'Perl Misc' started by Anton81, Jan 31, 2006.

  1. Anton81

    Anton81 Guest

    Hi,

    what is the easiest way to do substitutions in matched strings.
    For example I want to do s/ABC/DEF/g , but only if the ABC is between
    braces.

    Anton
     
    Anton81, Jan 31, 2006
    #1
    1. Advertising

  2. Anton81

    Paul Lalli Guest

    Anton81 wrote:
    > what is the easiest way to do substitutions in matched strings.
    > For example I want to do s/ABC/DEF/g , but only if the ABC is between
    > braces.


    http://search.cpan.org
    search for Regexp::Common::balanced

    Paul Lalli
     
    Paul Lalli, Jan 31, 2006
    #2
    1. Advertising

  3. Anton81

    Anton81 Guest

    >> what is the easiest way to do substitutions in matched strings.
    >> For example I want to do s/ABC/DEF/g , but only if the ABC is between
    >> braces.

    > s/{ABC}/{DEF}/g;


    ABC is not directly between braces. It's more something like

    s/({.*)ABC(.*})/\1DEF\2/g

    but as it get's more complicating (more different substitutions), one needs
    another solution.

    Anton
     
    Anton81, Jan 31, 2006
    #3
  4. Anton81 wrote:
    > >> what is the easiest way to do substitutions in matched strings.
    > >> For example I want to do s/ABC/DEF/g , but only if the ABC is between
    > >> braces.

    > > s/{ABC}/{DEF}/g;

    >
    > ABC is not directly between braces. It's more something like
    >
    > s/({.*)ABC(.*})/\1DEF\2/g
    >
    > but as it get's more complicating (more different substitutions), one needs
    > another solution.


    Actually, backreferences *within* the pattern (in the case of regex
    substitution, this is between the first two delimiters) are employed
    with a backslash and a digit--like your \1 and \2 for example. However,
    if you want to apply your pattern, and then do something with the
    captured strings (as is the case with your substitution) you want to
    use the $1 form of backreferences, where the '1' is the capture
    specified by the first left parens.

    See Programming Perl 3rd ed. pg. 183.
     
    it_says_BALLS_on_your forehead, Jan 31, 2006
    #4
  5. Anton81 wrote:
    > >> what is the easiest way to do substitutions in matched strings.
    > >> For example I want to do s/ABC/DEF/g , but only if the ABC is between
    > >> braces.

    > > s/{ABC}/{DEF}/g;

    >
    > ABC is not directly between braces. It's more something like
    >
    > s/({.*)ABC(.*})/\1DEF\2/g
    >
    > but as it get's more complicating (more different substitutions), one needs
    > another solution.


    what's wrong with:

    s/({.*?)ABC(.*?})/$1DEF$2/g;
     
    it_says_BALLS_on_your forehead, Jan 31, 2006
    #5
    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. Jonathan
    Replies:
    1
    Views:
    468
  2. RJGraham
    Replies:
    6
    Views:
    483
    RJGraham
    Jun 28, 2004
  3. crichmon
    Replies:
    4
    Views:
    486
    Mabden
    Jul 7, 2004
  4. Chris Goller
    Replies:
    4
    Views:
    376
    Jordan Stewart
    Mar 9, 2005
  5. Tony Eva
    Replies:
    1
    Views:
    481
    Jeff Epler
    Nov 9, 2003
Loading...

Share This Page