Question about "?" character in Perl Regular Expression

Discussion in 'Perl Misc' started by Ahmad, Jan 2, 2008.

  1. Ahmad

    Ahmad Guest

    When running the following example:

    $str="aaaaaa bbbb";
    ($a,$b)= $str=~/(\w+)\s?(\w+)/;
    print $a,"\n",$b;

    The result is:

    aaaaa
    a

    Why do we get this result??? Can anybody explain it?
    thanks and regards,
    Ahmad
     
    Ahmad, Jan 2, 2008
    #1
    1. Advertising

  2. Ahmad

    Paul Lalli Guest

    On Jan 2, 2:58 am, Ahmad <> wrote:
    > When running the following example:
    >
    > $str="aaaaaa   bbbb";
    > ($a,$b)= $str=~/(\w+)\s?(\w+)/;
    > print $a,"\n",$b;
    >
    > The result is:
    >
    > aaaaa
    > a
    >
    > Why do we get this result??? Can anybody explain it?


    Christian already explained to you the difference between ? and +, but
    perhaps you were also (or rather) confused about what \s actually is.
    You may be thinking that \s means simply "whitespace". It actually
    means "any single white-space character". It is any ONE character
    from the group: space, tab, newline, carriage return, or vertical
    tab. If you want to match more than one character in sequence, you
    must use the quantifiers (+, or *, or {n}, depending on your needs)

    Hope that helps,
    Paul Lalli
     
    Paul Lalli, Jan 2, 2008
    #2
    1. Advertising

  3. Ahmad <> wrote:
    >When running the following example:
    >
    >$str="aaaaaa bbbb";
    >($a,$b)= $str=~/(\w+)\s?(\w+)/;
    >print $a,"\n",$b;
    >
    >The result is:
    >
    >aaaaa
    >a
    >
    >Why do we get this result???


    What did you expect instead?

    jue
     
    Jürgen Exner, Jan 2, 2008
    #3
  4. Paul Lalli wrote:
    > On Jan 2, 2:58 am, Ahmad <> wrote:
    >> When running the following example:
    >>
    >> $str="aaaaaa bbbb";
    >> ($a,$b)= $str=~/(\w+)\s?(\w+)/;
    >> print $a,"\n",$b;
    >>
    >> The result is:
    >>
    >> aaaaa
    >> a
    >>
    >> Why do we get this result??? Can anybody explain it?

    >
    > Christian already explained to you the difference between ? and +, but
    > perhaps you were also (or rather) confused about what \s actually is.
    > You may be thinking that \s means simply "whitespace". It actually
    > means "any single white-space character". It is any ONE character
    > from the group: space, tab, newline, carriage return, or vertical
    > tab.


    Perl doesn't use the vertical tab. That should be the group: space,
    horizontal tab, new line, carriage return or form feed.


    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Jan 2, 2008
    #4
  5. Ahmad

    Alan_C Guest

    Ahmad wrote:

    > When running the following example:
    >
    > $str="aaaaaa bbbb";
    > ($a,$b)= $str=~/(\w+)\s?(\w+)/;
    > print $a,"\n",$b;
    >
    > The result is:
    >
    > aaaaa
    > a
    >
    > Why do we get this result??? Can anybody explain it?


    (how you've used it) Like they've replied the ? simply makes the \s optional
    as in it will match with or without \s in $str

    (not how you've used it) Yet another or one more use (.+? and .*? only) (I
    think) is to negate greed

    $str="aaaaaa -bbbb -ccc";
    ($a,$b)= $str=~/(\w+).+?-(\w+)/; # negate greed
    print $a,"\n",$b;
    ($a,$b)= $str=~/(\w+).+-(\w+)/;
    print "\n\n", $a,"\n",$b, "\n";
    # end

    --
    AC
     
    Alan_C, Jan 3, 2008
    #5
    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. VSK
    Replies:
    2
    Views:
    2,336
  2. MBow
    Replies:
    1
    Views:
    487
    Martin Honnen
    Jan 15, 2004
  3. Robert Brewer
    Replies:
    1
    Views:
    369
    Colin Brown
    Feb 23, 2004
  4. Eric @ Zomething
    Replies:
    0
    Views:
    364
    Eric @ Zomething
    Feb 23, 2004
  5. Diez B. Roggisch
    Replies:
    2
    Views:
    276
    Diez B. Roggisch
    Sep 5, 2005
Loading...

Share This Page