Remembering Matched Values

Discussion in 'Perl Misc' started by nicolas_laurent545@hotmail.com, Aug 5, 2005.

  1. Guest

    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
    , Aug 5, 2005
    #1
    1. Advertising

  2. Guest

    wrote:
    > 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.
    , Aug 5, 2005
    #2
    1. Advertising

  3. Paul Lalli Guest

    wrote:
    > 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
    Paul Lalli, Aug 5, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotrek \Alchemik\
    Replies:
    0
    Views:
    308
    Piotrek \Alchemik\
    Nov 12, 2006
  2. Nathan Sokalski

    Remembering values between postbacks

    Nathan Sokalski, Aug 20, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    492
    Eliyahu Goldin
    Aug 20, 2007
  3. Louisa99
    Replies:
    4
    Views:
    7,746
  4. CharitiesOnline.co.uk

    page remembering old values ...?

    CharitiesOnline.co.uk, Oct 6, 2003, in forum: ASP General
    Replies:
    1
    Views:
    101
    Ray at
    Oct 6, 2003
  5. Chandramohan Neelakantan

    Remembering part of last matched string

    Chandramohan Neelakantan, Oct 10, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    97
    Chandramohan Neelakantan
    Oct 17, 2003
Loading...

Share This Page