Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Regex Anchors confusion

by silent11 (Vicar)
on May 18, 2005 at 14:10 UTC ( #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"; }
Z:\>perl 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.

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://458216]
Approved by inman
[atcroft]: james28909: Make both hands into fists, place them together (with thumbs concealed), and every knuckle is a month (starting with Jan.) has 31 days, every dip (between knuckles) does not....
[atcroft]: (And Feb. is the odd case, because it is 28, unless it is a year divisible by 4, or if it is divisible by both 100 and 400 (at which point it is 29).)
[james28909]: i know but scroll through your calendar on your computer.
[james28909]: i thiught you were going to say make both hands into a fist and puch yourself in the face
[atcroft]: .oO(Sorry, I probably should have said take two normal hands....)
[atcroft]: james28909: No, unless you are a politician, I wouldn't say that (and even if you are, I still probably wouldn't say that).
[james28909]: i mean how hard can it be? its just subtracting days lol
[atcroft]: james28909: What about October 5, 1582?
[stevieb]: atcroft: "Make both hands into fists..."... is something my Ma taught me in our native lang, but I was to ignorant and young to pay attention. Thanks for that :)
[atcroft]: stevieb: Sad to say that I only recently learned that particular trick, but I have since found it very useful.... :)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2017-04-29 04:27 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (531 votes). Check out past polls.