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

I'm new to Perl. Awesome language!!! I'm having a problem with writing a matching expression. Since some of the substrings have "." in them, \b is not quite working for me.
#!/usr/local/bin/perl $teststr1='perl.org'; $teststr2='perl .org'; $termstr1='.org'; $termstr2='perl'; # I don't want to find 'perl' in 'perl.org' if ($teststr1 =~ m/\b$termstr2\b/) { print "uggh1\n"; } # I don't want to find '.org' in 'perl.org' if ($teststr1 =~ m/\b$termstr1\b/) { print "uggh2\n"; } # I DO want to find '.org' in 'perl .org' if ($teststr2 =~ m/\b$termstr1\b/) { print "hurray1\n"; } # I DO want to find 'perl' in 'perl .org' if ($teststr2 =~ m/\b$termstr2\b/) { print "hurray2\n"; } >...

Replies are listed 'Best First'.
Re: Help with matching .org
by moritz (Cardinal) on Jan 09, 2008 at 14:58 UTC
    Essentially you are searching for something that is delimited either by whitespaces or by end of string/line, right?

    In this case your regexp should look something like this:

    # custom word boundary: my $wb = qr{ ^ | $ | \s }x; # you have to take care if you are checking for a left or a right word + boundary: if ($str =~ m/(?<=$wb)$termstr1(?=$wb)/){ ... }
      Thanks! Delimited by whitespace, end of string/line, or beginning of string/line. Will this work for that too?
        hmmm. Perl does not like variable length lookaround. But it seems I can get away with (the less efficient):
        m/(^|?|\s)string(^|?|\s)/
        Thanks for your help!!!
Re: Help with matching .org
by poolpi (Hermit) on Jan 10, 2008 at 11:34 UTC
    <<Since some of the substrings have "." in them, \b is not quite working for me.>>
    $teststr1= quotemeta 'perl.org'; # 'perl\.org'

    HTH,
    PooLpi