Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Regex Anchors confusion

by silent11 (Vicar)
on May 18, 2005 at 14:10 UTC ( [id://458216]=perlquestion: print w/replies, xml ) Need Help??

silent11 has asked for the wisdom of the Perl Monks concerning the following question:

I am in the process of brushing up on my perl basics, reviewing Learning Perl, etc.

I am have a little question dealing with Regex anchoring patterns.

my $string = 'silent eleven'; if ($string =~ /\b \b/){ print "yes to 1\n"; } if ($string =~ /\b \b$/){ print "yes to 2\n"; } if ($string =~ /\b$/){ print "yes to 3\n"; }
Output:
Z:\>perl anchor.pl yes to 1 yes to 3
Why is the second if statement not returning true? Can these 2 "anchors" not be next to each other? Perhaps I am missing the point of using anchors. Any help or explanation would be appreciated.


-silent11
Spread Firefox

Replies are listed 'Best First'.
Re: Regex Anchors confusion
by merlyn (Sage) on May 18, 2005 at 14:21 UTC
    An anchor has no width: it's an assertion of a condition required for a successful match. For /^/ to be true, we must be at the beginning of a string, and so on. You can certainly have two anchors next to each other, provided both conditions are true. For example, to check that the beginning of the string is not also a word boundary (meaning the string starts with \W or is empty), we can use /^\B/.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Regex Anchors confusion
by inman (Curate) on May 18, 2005 at 14:23 UTC
    The second test is looking for a word boundary, followed by a space, followed by a word boundary, followed by the end of the string.

    The pattern doesn't match your data. I can't immediately think of a situation where / \b$/ would ever match.

      Since \b matches in between a word character and a non-word character, / \b$/ can never match.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
        Well, technically, \b matches between a word character, and the failure to match a word character (such as the edge of a string). I know you know that. This is for others that are following along at home. {grin}

        So, there are four pairs: word-word, word-nonword, edge-word, edge-nonword. \b matches the middle two of those, and \B matches the outer two of those.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: Regex Anchors confusion
by mrborisguy (Hermit) on May 18, 2005 at 14:19 UTC
    There's not a problem. That regex really doesn't find anything. \b is a word break, so you're looking for a word break, then a space, then a word break, then the end of your line. Well, you don't have that. You do have a word break and then a new line at the end, but there isn't a space before it. Do you understand what I'm saying? I don't know if that was the clearest explanation, but I hope it helps!

      I just wanted to point out something that your response seems to imply.

      You do have a word break and then a new line at the end, but there isn't a space before it.

      Even if he had a space before the newline it wouldn't match, because there wouldn't be a word break between the space and the end of the string. As inman says below, I can't think of any situation where / \b$/ would match. I'm not sure what it is about the word break anchor, but it seems to be the most misunderstood of the anchors. I know I had problems getting a grasp on it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://458216]
Approved by inman
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found