Replace <Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>

U

Udo

Hello,

I'm looking for an RegEx which replaces
<Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
with
<Word> <Pattern0> Literal <Word> <Pattern1> Literal ... <Word>
<PatternN>

Word is just a single word (not a literal)
,_ is a delimiter (just a ',' and optionally Spaces), not so important
<Pattern0> ... <PatternN> are always the same patterns.
Literal is always the same.

Main problem is the correct grouping for the backreferences.

Thanks and greetings
Udo
 
G

Gunnar Hjalmarsson

Udo said:
I'm looking for an RegEx which replaces
<Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
with
<Word> <Pattern0> Literal <Word> <Pattern1> Literal ... <Word>
<PatternN>

C:\home>type test.pl
my $string = '<Word>,_<Pattern0>,_<Pattern1>,_<Pattern2>';
my @words = ('', 'foo', 'bar');
$string =~ s/,_\s*(<Pattern(\d+)>)/ $words[$2] $1/g;
print $string, "\n";

C:\home>test.pl
<Word> <Pattern0> foo <Pattern1> bar <Pattern2>

C:\home>
 
A

Anno Siegel

Hello,

I'm looking for an RegEx which replaces

Strictly speaking, a regex doesn't replace things. A substitution
operator (s///),
the first part of which is a regex, replaces things.
<Word>,_<Pattern0>,_<Pattern1>,_ ... ,<PatternN>
with
<Word> <Pattern0> Literal <Word> <Pattern1> Literal ... <Word>
<PatternN>

Word is just a single word (not a literal)

What do you mean by "literal"?
,_ is a delimiter (just a ',' and optionally Spaces), not so important
<Pattern0> ... <PatternN> are always the same patterns.
Literal is always the same.

Main problem is the correct grouping for the backreferences.

It seems that in effect you want to change the delimiter to " Literal "
everywhere
in the string.

s/,\s*/ Literal /g;

Is that what you want?

Anno
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top