Remembering Matched Values

  • Thread starter nicolas_laurent545
  • Start date
N

nicolas_laurent545

This is my regular expression
s/(boy|girl)s/$1z/;
The $1 parameter will remember whichever word matches
Question:
How to make perl remember the last part s
(s) is a matching that has been replaced by z.
The purpose is to create output
Boys --- boyz
Girls -- girlz

Thanks
 
N

nobull

This is my regular expression
s/(boy|girl)s/$1z/;

No, the regular expression is just the bit between the first
pair of slashes.
The $1 parameter will remember whichever word matches
Yes.

Question:
How to make perl remember the last part s
(s)

Er, that is a question immediately followed by the answer.

But then given that the letter s is literal string why bother
to remember it? It will always be 's'.
is a matching that has been replaced by z.
The purpose is to create output
Boys --- boyz
Girls -- girlz

How is this in anyway related to the question you asked (and
then answered) about capturing the 's'?

Regex matching is by default case sensative. You'd either need
use a capital letter in the regex or make the matching case
insensative.

If you want to downcase the first caracter of $1
you can use \l in the RHS of the s///.

s/(boy|girl)s/\l$1z/i;

I'm not sure that's really the answer to the question you are
trying to ask. I think your problem is you haven't got clear in
your mind what you are trying to do.
 
P

Paul Lalli

This is my regular expression
s/(boy|girl)s/$1z/;
The $1 parameter will remember whichever word matches
Question:
How to make perl remember the last part s
(s) is a matching that has been replaced by z.
The purpose is to create output
Boys --- boyz
Girls -- girlz

There are several problems with this question:
1) That s/// will NOT make "Boys" into "boyz". It will make "boys"
into "boyz". Case matters.
2) The 's' is a literal string. You typed it into the code. What
makes you think you want Perl to remember it for you? You typed it
once. Just type it again. Perhaps you should give more information
about what you're really trying to do.
3) You already *know* how to capture and save parts of the regexp. You
did it when you capture and saved boy or girl. Why can't you do it
again?
s/(boy|girl)(s)/$1z/;
$1 ==> 'boy' or 'girl'
$2 ==> 's'

Paul Lalli
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top