Re: Substitutions in matched patterns?




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.

.



Relevant Pages

  • Re: Substitutions in matched patterns?
    ... >>> what is the easiest way to do substitutions in matched strings. ... > ABC is not directly between braces. ...
    (comp.lang.perl.misc)
  • Re: expr command and substitutions
    ... about the expr command and substitutions. ... tutorial says to always put the values being evaluated inside braces ... substitutions seem to work in this manner. ... How is it that the variable substitution happens inside the ...
    (comp.lang.tcl)
  • expr command and substitutions
    ... about the expr command and substitutions. ... tutorial says to always put the values being evaluated inside braces ... substitutions seem to work in this manner. ... How is it that the variable substitution happens inside the ...
    (comp.lang.tcl)
  • Re: Possible Tcl enhancements
    ... does one round of substitutions (one layer of braces). ...
    (comp.lang.tcl)