newbie pattern match question :-)

Discussion in 'Perl Misc' started by davidcsnow@yahoo.com, Jan 14, 2005.

  1. Guest

    I'm new to Perl, and am trying to match a string enclosed within
    parentheses, storing said string in a variable. Simple enough I would
    guess.

    An example of the text to search would be: "hello(world)", with the
    variable holding 'world' once done.

    How far off am I with:

    $text = "hello(world)";
    $new = $text =~ m/\(*\)/;
    Quite far offI am assuming!!

    Thanks in advance,
    David.
     
    , Jan 14, 2005
    #1
    1. Advertising

  2. wrote:
    > I'm new to Perl, and am trying to match a string enclosed within
    > parentheses, storing said string in a variable. Simple enough I would
    > guess.
    >
    > An example of the text to search would be: "hello(world)", with the
    > variable holding 'world' once done.
    >
    > How far off am I with:
    >
    > $text = "hello(world)";
    > $new = $text =~ m/\(*\)/;


    There are at least three things that need to be corrected:

    1) The '*' symbol is a quantifier in a Perl regular expression, meaning
    zero or more of the preceeding character or character class.
    Accordingly, you need to insert that character class.

    perldoc perlre

    2) You need parentheses to make the regex capture the desired string.

    3) Also $new need to be surrounded by parentheses in order to enforce
    list context.

    perldoc perlop (the m// operator)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jan 14, 2005
    #2
    1. Advertising

  3. wrote:

    > I'm new to Perl, and am trying to match a string enclosed within
    > parentheses, storing said string in a variable. Simple enough I would
    > guess.
    >
    > An example of the text to search would be: "hello(world)", with the
    > variable holding 'world' once done.
    >
    > How far off am I with:
    >
    > $text = "hello(world)";
    > $new = $text =~ m/\(*\)/;
    > Quite far offI am assuming!!
    >

    Close, but not quite. First problem: you are making a typical
    newbie mistake--confusing regexp syntax with the filematching
    or "globbing" syntax found in many shells (and, for that
    matter, in Perl when you do filename globbing). "*" doesn't
    mean "match any number of any character", it means "match
    any number (including zero) of the preceding character."
    so this matches zero to any number of left parens, followed
    by one right paren. In the string "hello(world)" your regexp
    will match ")". The rexexp character for "match any
    character (except newline)" is the period (.). So the
    expression you want for "match any number of any characters"
    is ".*".

    Second problem: m// doesn't return what it matched. Well,
    it can, but not in a scalar context, and the methods by which
    you can get it to return what's matched are not I think
    the most useful for what you're trying to do here. m//
    returns 1 if you got a match and "" if you didn't, which
    is handy if you're using it to check a conditional, but
    less so if you want to extract what was matched. There
    are a variety of ways to get what was matched, but for
    your case the best way is probably to use capturing
    parentheses, particularly since you *don't* want the
    whole match--you want to strip off the parentheses.
    This will do the trick:

    if ($text =~ m/\((.*)\)/) {
    $new = $1;
    }
    else {
    # it wasn't there!
    }

    After a successful match, the capturing parentheses
    populate the number variables--the first capture
    goes in $1, the second in $2 and so on. Note that
    these variables *only* get changed if the match
    was successful, which is why the code I gave
    makes the assignment only on a successful match.

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
     
    Chris Mattern, Jan 14, 2005
    #3
  4. Guest

    Thanks for the help guys - great advice there!!!

    The below does exactly what I was looking for, so thanks for the help,
    and for the advice on the doc.

    $text = "hello(world)";
    ($new) = $text =~ /\((.*)\)/;

    kind regards,
    David.
     
    , Jan 14, 2005
    #4
  5. wrote in news:1105742475.419709.52790
    @c13g2000cwb.googlegroups.com:

    > Thanks for the help guys - great advice there!!!
    >
    > The below does exactly what I was looking for, so thanks for the help,
    > and for the advice on the doc.
    >
    > $text = "hello(world)";
    > ($new) = $text =~ /\((.*)\)/;


    You might also benefit from reading

    perldoc -q matching

    Sinan
     
    A. Sinan Unur, Jan 14, 2005
    #5
  6. <> wrote:

    > The below does exactly what I was looking for,



    Have you tried it with a string like this yet?

    my $text = "hello(world) hello(dolly)";


    > $text = "hello(world)";
    > ($new) = $text =~ /\((.*)\)/;



    Why aren't there "my" declarations on those statements?

    You do have "use strict" turned on, don't you?



    You are likely to want non-greedy matching:

    my($new) = $text =~ /\((.*?)\)/;

    or greedy-but-not-paren matching:

    my($new) = $text =~ /\(([^)]*)\)/;

    which is altogether too ugly, so let's do an "x"treme makeover:

    my ($new) = $text =~ /
    \( # an open paren
    ( # memory 1 contains...
    [^)]* # ...chars that are not close parens
    ) # end of memory 1
    \) # a close paren
    /x;

    or, if you _want_ to get more that just the first one:

    my @new = $text =~ /
    \( # an open paren
    ( # memory 1 contains...
    [^)]* # ...chars that are not close parens
    ) # end of memory 1
    \) # a close paren
    /xg;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texa
     
    Tad McClellan, Jan 15, 2005
    #6
    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. hiwa
    Replies:
    0
    Views:
    653
  2. BCC

    Pattern match question

    BCC, Aug 23, 2004, in forum: C++
    Replies:
    6
    Views:
    430
    Alex Vinokur
    Aug 24, 2004
  3. Chris

    Pattern Match Question

    Chris, Jun 4, 2008, in forum: Java
    Replies:
    5
    Views:
    403
    Roedy Green
    Jun 4, 2008
  4. Rodney

    Pattern Match Question..

    Rodney, Sep 13, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    104
    Rodney
    Sep 14, 2003
  5. Allan M. Due
    Replies:
    5
    Views:
    112
    A. Sinan Unur
    Nov 13, 2005
Loading...

Share This Page