[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Omaha.pm] regex: \b Match a word boundary



Wow, love me some \b! Awesome! Thanks Trey!

Begin forwarded message:
> as a result of fuzzy matching pain I've become a fan of \b
> 
> my @names;
> push @names , "Blah Assn";
> push @names , " Blah Assn ";
> push @names , " Assn  Blah";
> push @names , "Assn  Blah";
> push @names , "Assnn  Blah";
> push @names , "AAssnn  Blah";
> push @names , "Blah AAssnn";
> 
> my $key='Assn';
> 
> foreach my $name ( @names ) {
>  if ( $name =~ /\b\Q$key\E\b/ ) {
>     print "matched |${name}|\n";
>  } else {
>     print "didn't match |${name}|\n";
>  }
> }

perldoc perlre

           \b  Match a word boundary
           \B  Match except at a word boundary

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".  (Within character classes
"\b" represents backspace rather than a word boundary, just as it normally does in any
double-quoted string.)