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

Is there a way to write a regex that fails if the line being compared has anything other than what is in the pattern? Reading that, it doesn't make much sense, so here is an example.
while (<>) { if ( /\w{5}\s\w{4}/ ) { print "This is a 5-letter word followed by a four-letter word\n"; } }
Now that would match this:

happy dude

but it would also match this:

man, that's a happy dude

Is there some way I can make the regex match ONLY 5-letter words followed by 4-letter words, but not ANYTHING else (so that it would match "happy dude" but NOT "man, that's a happy dude")? In other words, can I force the regex to say "the entire line being compared, not just a substring, has to match this pattern"?

Some people drink from the fountain of knowledge, others just gargle.

Replies are listed 'Best First'.
Re: "Complete match" regexes
by arhuman (Vicar) on Feb 02, 2001 at 20:18 UTC
    Use  ^ and $ in your regex (for the beginning and the end of the line) :

    while (<>) { if ( /^\w{5}\s\w{4}$/ ) { print "This is a 5-letter word followed by a four-letter word\n"; } }

    BTW: more details in perldoc perlre (it's a must read ;-)
      You might want to use the \z anchor instead of the $ anchor -- \z is explicit end-of-string, but $ can match right before a newline at the end of a string.

      japhy -- Perl and Regex Hacker
      If you aren't fussed about leading and trailing spaces, you can modify the regex thusly:
      /^\s*\w{5}\s\w{4}\s*$/
      </code> Update: Yeah, I know the question was pretty explicit that he wanted to match exactly a 5 letter word followed by a 4 letter word. The question had already been answered so I posted this in case it's of use to anyone else.

      It happens occasionally that you think - I want a regex to allow x and and make it too unforgiving to allow for mistakes in user input. Correct me if I'm wrong, but I don't think the code I posted is incorrect?

      Update 2: Fastolfe pointed out that I used character classes when I didn't need them so I took them out. He was quite right - not sure why I used them, but I don't think they did any harm, I originally posted: /^[\s]*\w{5}\s\w{4}[\s]*$/

      $code or die
      Using perl at
      The Spiders Web
Re: "Complete match" regexes
by azatoth (Curate) on Feb 02, 2001 at 20:37 UTC
    You could also look at \b in pattern matching. Use \bword\b to patch the word and _nothing_ else...

    UPDATE: Chromatic has pointed this one out :)
    chromatic says Re: "Complete match" regexes has an incorrect explanation. \b matches word *boundaries*. A regex not anchored can still match other things.


    <font color="#0000ff"<Azatoth a.k.a Captain Whiplash

    Get YOUR PerlMonks Stagename here!
    Want to speak like a Londoner?
A reply falls below the community's threshold of quality. You may see it by logging in.