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

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

am 02.09.2007 01:00:33 von udo

Hello,

I'm looking for an RegEx which replaces
,_,_,_ ... ,
with
Literal Literal ...


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

Main problem is the correct grouping for the backreferences.

Thanks and greetings
Udo

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

am 02.09.2007 01:41:09 von Gunnar Hjalmarsson

Udo wrote:
> I'm looking for an RegEx which replaces
> ,_,_,_ ... ,
> with
> Literal Literal ...
>

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

C:\home>test.pl
foo bar

C:\home>

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

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

am 02.09.2007 02:31:41 von Anno Siegel

On 2007-09-02 01:00:33 +0200, Udo said:

> 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.

> ,_,_,_ ... ,
> with
> Literal Literal ...
>
>
> 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
> ... 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