develooper Front page | perl.perl5.porters | Postings from January 2016

[perl #127436] fail to find a match with a regular expression

Thread Previous
From:
James E Keenan via RT
Date:
January 31, 2016 23:10
Subject:
[perl #127436] fail to find a match with a regular expression
Message ID:
rt-4.0.18-28447-1454281833-1041.127436-15-0@perl.org
On Sat Jan 30 12:50:29 2016, spz1st@gmail.com wrote:
> 
> This is a bug report for perl from spz1st@gmail.com,
> generated with the help of perlbug 1.39 running under perl 5.18.2.
> 
> 
> -----------------------------------------------------------------
> Should the regular expression "\bmr\.\b" and "\bmr.\b" match "mr." in
> the string
>  "a mr. cbcmr"?
> 
> % perl -e '$str = "a mr. cmrbcmr"; $str =~ s/\bmr.\b/MR/g; print $str,
> "\n";'
> a mr. cmrbcmr
> % perl -e '$str = "a mr. cmrbcmr"; $str =~ s/\bmr\.\b/MR/g; print
> $str, "\n";'
> a mr. cmrbcmr
> % perl -e '$str = "a mr. cmrbcmr"; $str =~ s/\bmr\./MR/g; print $str,
> "\n";'
> a MR cmrbcmr
> % perl -e '$str = "a mr. cmrbcmr"; $str =~ s/\bmr\b/MR/g; print $str,
> "\n";'
> a MR. cmrbcmr
> % perl -e '$str = "a mr. cmrbcmr"; $str =~ s/\bmr/MR/g; print $str,
> "\n";'
> a MR. cmrbcmr
> % perl -e '$str = "a mr. cmrbcmr"; $str =~ s/mr\b/MR/g; print $str,
> "\n";'
> a MR. cmrbcMR
> % perl -e '$str = "a mr. cmrbcmr"; $str =~ s/mr/MR/g; print $str,
> "\n";'
> a MR. cMRbcMR
> 

You are asking a question about pattern matching, but you're confusing matters by providing examples using substitutions.  Let's start by simply focusing on the pattern matching.  See first attachment:  127436-match.pl

Running this program gives this output:
#####
NO
NO
YES
YES
YES
YES
YES
#####

The reason why there is no pattern matched in the first two examples lies in the way '\b' is precisely defined in 'perldoc perlre'.

#####
A word boundary ("\b") is a spot between two characters that has a "\w" on one side of it and a "\W" on the other side of it (in either order), counting the imaginary characters off the beginning and end of the string as matching a "\W".
#####

The '.' in the string is not a '\w'; it's a \W'.  So what you have there is two consecutive '\W' characters -- not a '\w' followed by a '\W'.  This can be seen more clearly in the second attached file: 127436-reduced.pl.  This file produces:

#####
NO
NO

NO
YES
#####

No bug in Perl here.  Thank you very much.

-- 
James E Keenan (jkeenan@cpan.org)

---
via perlbug:  queue: perl5 status: new
https://rt.perl.org/Ticket/Display.html?id=127436

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About