Substitutions in matched patterns?

A

Anton81

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
 
A

Anton81

what is the easiest way to do substitutions in matched strings.
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
 
I

it_says_BALLS_on_your forehead

Anton81 said:
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.
 
I

it_says_BALLS_on_your forehead

Anton81 said:
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;
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top