How can I interpolate the RHS of a regex?

M

Mr P

I'm trying to process strings like:

cat_7 becomes dog_7....

so for the general numeric case it's something like

s/cat_(\d)/dog_$1/;

However, I don't know what string I may be asked to match/substitute
on. Therefore I'm trying to store the LHS and RHS in a var, and
evaluate them:

$k = 'cat_(\d+)';
$k2 = 'dog_$1';
$line =~ s/$k/$k2/e;

The LHS seems ok- it properly finds a like like cat_7. However, the RHS
does not substitute the value of $1- instead it always produces
'dog_$1' instead of 'dog_7'.

I tried some other regex switches too. I also read the Perlre info
where it says the RHS is interpreted as a double-quoted string. Oddly,
this seems to be a single-quoted string to me.

Can someone please tell me what I'm missing? I thought the /e would
tell it to evaluate $1 ?

Thank-You and Happy Holiday,
Mister P
 
U

usenet

Mr said:
$k2 = 'dog_$1';

There's your problem. Single quotes don't interpolate the string. So $1
is being represented as a string literal. Use double-quotes here.
 
J

John Bokma

There's your problem. Single quotes don't interpolate the string. So $1
is being represented as a string literal. Use double-quotes here.

No: $1 is undefined (probably) at that stage. It's very unlikely that it
contains the right value.
 
J

Jürgen Exner

Mr P wrote:
[Subject: How can I interpolate the RHS of a regex?]

There is no such thing as a Right Hand Side in a Regular Expression.

jue
 
T

Tad McClellan

Mr P said:
I'm trying to process strings like:

cat_7 becomes dog_7....

so for the general numeric case it's something like

s/cat_(\d)/dog_$1/;

However, I don't know what string I may be asked to match/substitute
on. Therefore I'm trying to store the LHS and RHS in a var, and
evaluate them:

$k = 'cat_(\d+)';
$k2 = 'dog_$1';
$line =~ s/$k/$k2/e;

The LHS seems ok- it properly finds a like like cat_7. However, the RHS
does not substitute the value of $1- instead it always produces
'dog_$1' instead of 'dog_7'.

I tried some other regex switches too. I also read the Perlre info
where it says the RHS is interpreted as a double-quoted string.


Once you know that, then you can find the FAQ entry that
addresses your problem:

perldoc -q string

How can I expand variables in text strings?

Oddly,
this seems to be a single-quoted string to me.


The RHS is _normally_ a double quotish string.

The s///e modifier, errr, modifies that so that the RHS
is now Perl code rather than a string.

Can someone please tell me what I'm missing?


A 2nd 'e' modifier, so that you'll get another round of evaluation.

I thought the /e would
tell it to evaluate $1 ?


No, /e tells it to evaluate $k2.
 
E

Eric J. Roode

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
And I just noticed this... you need to do this as a qr{} instead of a
string... see perldoc perlre and look for 'qr'

No he doesn't.

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
M

Mr P

Thank-You, I actually already tried "ee", and I got a slew of errors
such as "cannot ealuate "cat".. and so on..


But perhaps the '"expression"' is needed?

Is there a better way to do this? This actually seems logcally to be
what I want to do (stuff in the string and insert the digits)..

Gracias
 
D

David H. Adler

OK jue, the right "side", sorry..

2 points:

1) some attribution and quoting would be nice (please see the
twice-weekly posted Posting Guidelines elsewhere in this newsgroup).

2) If I remember correctly (without quoting, I'm going by memory here),
what was said was "There is no such thing as a 'right side' of a regular
expression". This still holds. I *imagine* that you were referring to
the right side of a *substitution operation*, rather than a regex.

dha
 
B

Bart Lateur

Mr said:
OK jue, the right "side", sorry..

What he means s that the "right hand side" of a s/// isn't a regular
expression. Only the LHS is. The RHS is a doublequotish interpolated
string -- or code if you use /e.
 
M

Mr P

David said:
2 points:

1) some attribution and quoting would be nice (please see the
twice-weekly posted Posting Guidelines elsewhere in this newsgroup).

2) If I remember correctly (without quoting, I'm going by memory here),
what was said was "There is no such thing as a 'right side' of a regular
expression". This still holds. I *imagine* that you were referring to
the right side of a *substitution operation*, rather than a regex.

dha



I was posting to this newsgroup on a VT240 when there were no graphical
interfaces to newsgroups at all, I don't need nor do I accept your
posting tutorials or advice. Thanks anyhow.

As for your RHS meta-discussion, its obvious that given s/x/y/ , y is
to the right of x. Perhaps in some rigid definition of RHS, that's
formally not true. But I think its evident to anyone reading this
thread (except perhaps you) what RHS means.

Q.E.D.
 
M

Mr P

Thanks Bart, understood. I wasn't using "right side" in any formal
sense..

Turns out I can use "ee" with some other adjustments.

Merry Christmas dude!
 
T

Tad McClellan

[ snip quoted .sig ]
I was posting to this newsgroup on a VT240


I'm sure we are all impressed by your age and experience, but
didn't they quote context and delete .sigs and whatnot even then?

when there were no graphical
interfaces to newsgroups at all,


How is the form of the interface relevant?

You are expected to follow the conventions in composing followups
whether you are using a tty, a GUI, or whistling into a modem.

I don't need


But you do (did) need it, you just don't recognize what benefit following
David's advice might have.

1) You need to compose followups normally to avoid having all of
your future posts auto-ignored.

2) You need to understand the difference between a "regular expression"
and the "substitution operator" in order to answer your original question.

nor do I accept


You are on your own then. Have the appropriate amount of fun.

your
posting tutorials or advice.


Don't post to usenet if you don't want to get advice.

Thanks anyhow.


Your disingenuousness has earned you eternal invisiblity.

So long!

As for your RHS meta-discussion,

which was discussing your subject header:

Subject: How can I interpolate the RHS of a regex?
its obvious that given s/x/y/ , y is
to the right of x.


Yes but y is NOT A REGEX. You said "of a regex".

x is a regex. what is the "right hand side" of x ?

Perhaps in some rigid definition of RHS, that's
formally not true. But I think its evident to anyone reading this
thread (except perhaps you) what RHS means.


You seem to have misidentified the point.

The point is not about "sidedness".

The point is about what part of s/x/y/ is a regex and what part
is a substitute operator.



If you had this distinction clear, then you could have reasoned
that the RHS of the _substitution operator_ is a string (not
a regex) and used "string" to find the answer in the Perl FAQ
straightaway without relying on others.
 
A

axel

Mr P said:
I was posting to this newsgroup on a VT240 when there were no graphical
interfaces to newsgroups at all, I don't need nor do I accept your
posting tutorials or advice. Thanks anyhow.

That's interesting... I thought this newsgroup was about 10 to 12 years old...
it must be far older as I remember using xrn around 1990.

Axel
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top