Re: Substitutions in matched patterns?
- From: "it_says_BALLS_on_your forehead" <simon.chao@xxxxxxx>
- Date: 31 Jan 2006 06:46:32 -0800
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.
.
- References:
- Substitutions in matched patterns?
- From: Anton81
- Re: Substitutions in matched patterns?
- From: Bernard El-Hagin
- Re: Substitutions in matched patterns?
- From: Anton81
- Substitutions in matched patterns?
- Prev by Date: Perl 6.x , please offer PREPEND as well as APPEND
- Next by Date: Re: Perl 6.x , please offer PREPEND as well as APPEND
- Previous by thread: Re: Substitutions in matched patterns?
- Next by thread: Re: Substitutions in matched patterns?
- Index(es):
Relevant Pages
|