regex question

Discussion in 'Perl Misc' started by inderpaul_s@yahoo.com, Apr 13, 2006.

  1. Guest

    I have the following code...my only question is in line 4. where you
    see the ^ and $ (last one) character which means start/end of the
    string. But my question is where does it stop matching for the caret
    and the dollar sign ?

    I think the caret is everything from the beginning of the string all
    the way to the end and same when matching from the end of the line it
    matches everything to the start of the string. There is nothing to tell
    it to stop ? Does this sound correct ?


    1. my $line = "jsmith:password \n";
    2. my $user = "jsmith";
    3. my $password = "password";

    4. if($line =~ /^$user:$password$/)

    5. {print "user name and password match\n";}
    6. else{print "user name and password does NOT match\n";}
     
    , Apr 13, 2006
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > I have the following code...my only question is in line 4. where you
    > see the ^ and $ (last one) character which means start/end of the
    > string. But my question is where does it stop matching for the caret
    > and the dollar sign ?


    I don't understand what you're asking. What does "stop matching" mean?
    There is no start or stop to the matching. The ^ matches exactly once
    - at the beginning of the string. The $ matches exactly once - at the
    end of the string [1].

    > I think the caret is everything from the beginning of the string all
    > the way to the end and same when matching from the end of the line it
    > matches everything to the start of the string.


    No, the caret matches ONLY the beginning of the string. It does not
    match any characters that follow the beginning of the string. Same
    applies to the $ for the end.

    > There is nothing to tell
    > it to stop ? Does this sound correct ?


    No, because there is no concept of "stopping". I think you are
    confusing the ^ and $ anchors with the + or * quantifiers.

    > 4. if($line =~ /^$user:$password$/)


    This says: "If $line matches the beginning of the string, the variable
    $user, a colon, the variable $password, and the end of the string".
    That is, $line must contain *nothing* but $user, colon, $password.
    Nothing can come before or after that, or the pattern match fails.

    Paul Lalli
     
    Paul Lalli, Apr 13, 2006
    #2
    1. Advertising

  3. Guest

    "" <> wrote:
    > I have the following code...my only question is in line 4. where you
    > see the ^ and $ (last one) character which means start/end of the
    > string. But my question is where does it stop matching for the caret
    > and the dollar sign ?


    It doesn't. Since every string has a beginning and an end, ^ and $
    in isolation will always match.

    >
    > I think the caret is everything from the beginning of the string all
    > the way to the end and same when matching from the end of the line it
    > matches everything to the start of the string.


    The ^ matches the start. The $ matches the end, or the newline right
    before the end.

    > There is nothing to tell
    > it to stop ?


    Sure, the rest of the regexp.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Apr 14, 2006
    #3
  4. Dr.Ruud Guest

    schreef:

    > 4. if($line =~ /^$user:$password$/)


    Both $user and $password could contain 'special' characters like
    ..*?()[]{}\
    so you should use \Q to get those quoted (see perldoc perlre).

    4. if($line =~ /^\Q$user:$password$\E/)

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Apr 14, 2006
    #4
  5. robic0 Guest

    On 13 Apr 2006 15:46:07 -0700, "" <> wrote:

    >I have the following code...my only question is in line 4. where you
    >see the ^ and $ (last one) character which means start/end of the
    >string. But my question is where does it stop matching for the caret
    >and the dollar sign ?
    >
    >I think the caret is everything from the beginning of the string all
    >the way to the end and same when matching from the end of the line it
    >matches everything to the start of the string. There is nothing to tell
    >it to stop ? Does this sound correct ?
    >
    >
    >1. my $line = "jsmith:password \n";
    >2. my $user = "jsmith";
    >3. my $password = "password";
    >
    >4. if($line =~ /^$user:$password$/)

    ^^^^^^ ^^^^^^^^^^
    The carret at the beggining denotes that the next char
    after it must be the first char in the line.
    The dollar sign at the end denotes the first previous char
    must be at the end.

    Thats all it does. Everything inbetween is relative only to each other.
    The jist of regexp is that every char is juxta-positioned to every other
    character in the matching string. There are various anchor characters
    line the ones your using for beginning and end.

    >
    >5. {print "user name and password match\n";}
    >6. else{print "user name and password does NOT match\n";}
     
    robic0, Apr 14, 2006
    #5
  6. robic0 Guest

    On Thu, 13 Apr 2006 16:13:02 -0700, robic0 wrote:

    >On 13 Apr 2006 15:46:07 -0700, "" <> wrote:
    >
    >>I have the following code...my only question is in line 4. where you
    >>see the ^ and $ (last one) character which means start/end of the
    >>string. But my question is where does it stop matching for the caret
    >>and the dollar sign ?
    >>
    >>I think the caret is everything from the beginning of the string all
    >>the way to the end and same when matching from the end of the line it
    >>matches everything to the start of the string. There is nothing to tell
    >>it to stop ? Does this sound correct ?
    >>
    >>
    >>1. my $line = "jsmith:password \n";
    >>2. my $user = "jsmith";
    >>3. my $password = "password";
    >>
    >>4. if($line =~ /^$user:$password$/)

    > ^^^^^^ ^^^^^^^^^^
    >The carret at the beggining denotes that the next char
    >after it must be the first char in the line.
    >The dollar sign at the end denotes the first previous char
    >must be at the end.
    >
    >Thats all it does. Everything inbetween is relative only to each other.
    >The jist of regexp is that every char is juxta-positioned to every other
    >character in the matching string. There are various anchor characters
    >line the ones your using for beginning and end.
    >
    >>
    >>5. {print "user name and password match\n";}
    >>6. else{print "user name and password does NOT match\n";}

    I will break down regexp won notch further. The *thing*, no matter
    what *it* is (in its primitive state, is a character), in regexp,
    it is only relative to whats before it and what comes after it.
    Consider the regexp engine as walking through a bunch of *things*
    framed by whats before and after the *thing*, then proceeds to the
    next *thing*. When you have resolved all the lowest level *thing*'s, you
    have to put it into a group. The group becomes the new *thing*.
    Wash, rinse and repeat........
     
    robic0, Apr 14, 2006
    #6
  7. Guest

    ok i understand what your saying...if you start at the beginning of
    $line and it contains "jsmith:password" and same if you start at the
    end of $line and it also contains the string "jsmith:password" so the
    if statement evaulates to true

    thanks again to everyone
     
    , Apr 14, 2006
    #7
  8. robic0 Guest

    On 13 Apr 2006 17:19:30 -0700, "" <> wrote:

    >ok i understand what your saying...if you start at the beginning of
    >$line and it contains "jsmith:password" and same if you start at the
    >end of $line and it also contains the string "jsmith:password" so the
    >if statement evaulates to true
    >
    >thanks again to everyone

    Good, though maybe you would refreain from using "regex question" in your
    subject line since its used multiple times in this news group.
     
    robic0, Apr 14, 2006
    #8
  9. Guest

    ok sure...will do for next time. thanks.
     
    , Apr 14, 2006
    #9
  10. Dr.Ruud wrote:
    > schreef:
    >
    >>4. if($line =~ /^$user:$password$/)

    >
    > Both $user and $password could contain 'special' characters like
    > .*?()[]{}\
    > so you should use \Q to get those quoted (see perldoc perlre).
    >
    > 4. if($line =~ /^\Q$user:$password$\E/)


    You have the \E in the wrong place.

    $ perl -le' $_ = qr/^password$/; print; $_ = qr/^\Qpassword$\E/; print'
    (?-xism:^password$)
    (?-xism:^password\
    E)


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Apr 14, 2006
    #10
  11. Dr.Ruud wrote:
    > schreef:
    >
    >> 4. if($line =~ /^$user:$password$/)

    >
    > Both $user and $password could contain 'special' characters like
    > .*?()[]{}\
    > so you should use \Q to get those quoted (see perldoc perlre).
    >
    > 4. if($line =~ /^\Q$user:$password$\E/)


    Well, I surely hope the OP expects $user and $password to contain special
    characters.

    If you want to match literally then
    if ($line eq "$user:$password")
    would be much easier to write and to read.

    jue
     
    Jürgen Exner, Apr 14, 2006
    #11
  12. robic0 Guest

    On Fri, 14 Apr 2006 01:37:15 GMT, "Jürgen Exner" <> wrote:

    >Dr.Ruud wrote:
    >> schreef:
    >>
    >>> 4. if($line =~ /^$user:$password$/)

    >>
    >> Both $user and $password could contain 'special' characters like
    >> .*?()[]{}\
    >> so you should use \Q to get those quoted (see perldoc perlre).
    >>
    >> 4. if($line =~ /^\Q$user:$password$\E/)

    >
    >Well, I surely hope the OP expects $user and $password to contain special
    >characters.
    >
    >If you want to match literally then
    > if ($line eq "$user:$password")
    >would be much easier to write and to read.
    >
    >jue
    >

    This is correct, but you must tell him why he thinks he needs regexp
    and how his regexp is not adequate. Regexp is afterall a notion
    that the literal is not adequate, nor trusted.
     
    robic0, Apr 14, 2006
    #12
  13. Guest

    ok sure...will do for next time. thanks.
     
    , Apr 14, 2006
    #13
  14. robic0 Guest

    On Fri, 14 Apr 2006 01:09:50 GMT, "John W. Krahn" <> wrote:

    >Dr.Ruud wrote:
    >> schreef:
    >>
    >>>4. if($line =~ /^$user:$password$/)

    >>
    >> Both $user and $password could contain 'special' characters like
    >> .*?()[]{}\
    >> so you should use \Q to get those quoted (see perldoc perlre).
    >>
    >> 4. if($line =~ /^\Q$user:$password$\E/)

    >
    >You have the \E in the wrong place.
    >
    >$ perl -le' $_ = qr/^password$/; print; $_ = qr/^\Qpassword$\E/; print'
    >(?-xism:^password$)
    >(?-xism:^password\
    >E)
    >
    >
    >John


    Here we go again.... The 'what ifs'...
    Here's one for you kahnn... What if '\Q','\E' do not cover all escape
    characters? Read the docs, they don't!

    This is complete, but some char's are not necessary, posted over and over and
    over again.... Capitan cut'n paste

    sub convertPatternMeta
    {
    my ($pattern) = shift;
    my @regx_esc_codes =
    (
    "\\", '/', '(', ')', '[', ']', '?', '|',
    '+', '.', '*', '$', '^', '{', '}', '@'
    );
    foreach my $tc (@regx_esc_codes) {
    # code template for regex
    my $xxx = "\$pattern =~ s/\\$tc/\\\\\\$tc/g;";
    eval $xxx;
    if ($@) {
    # the compiler will show the escape char, add
    # it char to @regx_esc_codes
    $@ =~ s/^[\s]+//s; $@ =~ s/[\s]+$//s;
    die "$@";
    }
    }
    return $pattern;
    }
     
    robic0, Apr 14, 2006
    #14
  15. robic0 Guest

    On 13 Apr 2006 18:52:57 -0700, "" <> wrote:

    >ok sure...will do for next time. thanks.


    Hehehe, can you ever get out of this thread alive?
     
    robic0, Apr 14, 2006
    #15
  16. wrote:
    > ok sure...will do for next time. thanks.


    What the f*** will you do next time?

    Please quote appropriate context -as has been a proven custom in Usenet for
    over 2 decades- such that people have a chance to know what you are talking
    about.

    jue
     
    Jürgen Exner, Apr 14, 2006
    #16
  17. Dr.Ruud Guest

    John W. Krahn schreef:
    > Dr.Ruud:
    >> :


    >>> 4. if($line =~ /^$user:$password$/)

    >>
    >> Both $user and $password could contain 'special' characters like
    >> .*?()[]{}\
    >> so you should use \Q to get those quoted (see perldoc perlre).
    >>
    >> 4. if($line =~ /^\Q$user:$password$\E/)

    >
    > You have the \E in the wrong place.
    >
    > $ perl -le' $_ = qr/^password$/; print; $_ = qr/^\Qpassword$\E/;
    > print' (?-xism:^password$)
    > (?-xism:^password\
    > E)


    Yes. I was first going to leave it out since it is not needed. Than I
    inserted it in the wrong position...

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Apr 14, 2006
    #17
  18. <> wrote:

    > ok i understand what your saying...



    No you don't. Keep trying.

    Anchors (eg. ^ and $ ) do not affect where in the string
    you "start" attempting to match.

    Anchors match a *position in the string*.


    [ Please quote some context in followups like everybody else does.
    Please start doing this very soon.
    Thank you.
    ]


    > if you start at the beginning of
    > $line



    Pattern matching *always* "starts" at the beginning of the string.


    > and it contains "jsmith:password" and same if you start at the
    > end of $line



    Pattern matching *never* "starts" at the end of the string.


    > and it also contains the string "jsmith:password" so the
    > if statement evaulates to true



    What "if statement"?

    Please quote some context in followups like everybody else does.

    /^jsmith:password$/

    matches:
    beginning of string followed by
    the character "j" followed by
    the character "s" followed by
    the character "m" followed by
    ...
    the character "o" followed by
    the character "r" followed by
    the character "d" followed by
    the end of string (or just before a newline at the end of string)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 14, 2006
    #18
    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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    713
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,635
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    601
  4. Xah Lee
    Replies:
    1
    Views:
    945
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    772
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page