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

I am trying to use the regular expression start of word i.e \< but it is not working. Can you please help me out with this? My program is as follows but I am getting answer 'no'. #!/usr/bin/perl -w use strict; my $string = " cat mat rat pat\n"; if ($string =~ /\<cat/) { print "yes\n"; } else { print "no\n"; } exit 0;

Replies are listed 'Best First'.
Re: regex start of word (\<)
by Corion (Patriarch) on Feb 01, 2011 at 12:49 UTC

    Where in perlre, the documentation for Perl Regular Expressions did you find that \< means "start of word"? I only find one mention of \<, and that's in the sentence:

    So anything that looks like \\, \(, \), \<, \>, \{, or \} is always interpreted as a literal character, not a metacharacter.

    .. and that seems to me that it specifically rules out that \< has any meaning beyond a literal <.

Re: regex start of word (\<)
by ww (Archbishop) on Feb 01, 2011 at 13:02 UTC
    I can't tell you with absolute certainty that Perl regexen don't use \< and \> (even though some others do, according to Friedl, Master Regular Expressions, p131) but if you substitute \b -- which Perl does recognize, your script works.
    #!/usr/bin/perl use strict; use warnings; #885479 my $string = " cat mat rat pat\n"; if ($string =~ /\bcat/) { print "yes\n"; } else { print "no\n"; }

    Note also the omission of your exit 0; and substitution of use warnings (scoped to the current script whereas  - w may kick out warnings about any modules you might use in some more complex program.

    However, for future posts, please enclose code (and data) in <c>...</c> tags. See Markup in the Monastery.

    Updated with MRE citation.

Re: regex start of word (\<)
by JavaFan (Canon) on Feb 01, 2011 at 13:58 UTC
    Perl ain't vi.

    Perl uses \b to signify a place between a "word" and a "non-word" character, which is somewhat different from vi's notion. And Perl doesn't distinguish between "begin word" and "end word".

Re: regex start of word (\<)
by biohisham (Priest) on Feb 01, 2011 at 13:39 UTC
    Your string looks like " cat mat rat pat\n", you wanna match the word 'cat' and not 'cater' or 'cats' or 'Catrina' for example, so your regex has to account for matching the word cat bounded by spaces or non word characters.. So you have many likely regex constructions to pick from (not including the look-ahead or look-behind assertions or the backtracking or the backreferences):
    $string =~ /\bcat\b/ $string =~ /\Wcat\W/ $string =~ /\scat\s/
    If you don't mind the letter-case of the word then you can use the i switch as in <c>m/\bcat\b/i<c>, read perlretut for a tutorial on regular expressions..

    Update: another interesting read is 7 Stages of Regex Users...


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
Re: regex start of word (\<)
by elagon (Initiate) on Feb 01, 2011 at 13:22 UTC
    Umm, take a look at the official docs at http://perldoc.perl.org/perlre.html