Home > Archive > PERL Beginners > January 2006 > complex replacement with regexp...
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
complex replacement with regexp...
|
|
| pierre_catello@yahoo.fr 2006-01-30, 6:56 pm |
| Hello everyone,
I'm wondering what is the "recommended" way to perform conditional
replacements (or replacement depending on some arbitrary complex logic)
using regular expression matching, when s///e cannot be used.
What I try to do is something like :
while ($input =~ /<tag attrib1="(left|right|center)"
attrib2="(.+?)".*?>(.+?)<\/tag>/sg) {
compute the replacement of $3 depending on the value of $1 and $2
}
The problem is that the matching group are mapped on the variables $1,
$2, ... $n, but these variable are read-only. So one cannot do
something like
$3 = my_replacement;
Is there an "easy way" to modifiy the matched group ?
Currently, I use an ugly thing consisting on getting the match index in
$input with $-[i]/$+[i] and then use substr to do the replacement I
need, like in the following :
substr ($input, $-[3], $+[3]-$-[3], $my_replacement);
Thanks in advance for any help or pointers to some relevant docs
Pierre
| |
| Xicheng 2006-01-30, 6:56 pm |
| pierre_catello@yahoo.fr wrote:
> Hello everyone,
>
> I'm wondering what is the "recommended" way to perform conditional
> replacements (or replacement depending on some arbitrary complex logic)
> using regular expression matching, when s///e cannot be used.
>
> What I try to do is something like :
>
> while ($input =~ /<tag attrib1="(left|right|center)"
> attrib2="(.+?)".*?>(.+?)<\/tag>/sg) {
> compute the replacement of $3 depending on the value of $1 and $2
> }
>
> The problem is that the matching group are mapped on the variables $1,
> $2, ... $n, but these variable are read-only. So one cannot do
> something like
> $3 = my_replacement;
> Is there an "easy way" to modifiy the matched group ?
I am not quite sure about your requirement, but if you re-organize the
parenthesis in your regex pattern I think you can write s///e in the
following form:
$input =~ s/(<tag attrib1="(left|right|center)"
attrib2="(.+?)".*?> ).+?(<\/tag> )/$1.func($2,$3).$4/esg;
then apply any complex logic in that func() based on two captured
values $2 and $3, like
sub func {
if ($_[0] eq 'left' and $_[1] eq 'bla') {
#return sth;
} else (blabla) {
#return sth
}
HTH,
Xicheng
>
> Currently, I use an ugly thing consisting on getting the match index in
> $input with $-[i]/$+[i] and then use substr to do the replacement I
> need, like in the following :
>
> substr ($input, $-[3], $+[3]-$-[3], $my_replacement);
>
> Thanks in advance for any help or pointers to some relevant docs
>
> Pierre
| |
| pierre_catello@yahoo.fr 2006-01-30, 6:56 pm |
|
Thanks a lot! I didn't knew that it was possible to call a function
inside a s///e expression. This fits my need :-) Thanks again!
Pierre
|
|
|
|
|