What's wrong with \b? It's only needed at the beginning, since you have the closing quote to mark the end. (UPDATED below with fancier pattern)
Test code:
Produces:#!/usr/bin/perl use strict; use warnings; use diagnostics; my $alpha = qq(alpha="first"); my $beta = qq(beta="second"); my $gamma = qq(gamma="third"); my $s1 = qq($alpha $beta $gamma); my $s2 = qq($alpha $gamma $beta); my $s3 = qq($beta $alpha $gamma); #my $pat = qr/\b(beta="second")/; my $pat = qr/(?:\A|\s)(beta="second")(?:\s|\Z)/; print "s1: \'$s1\', match=".($s1 =~ $pat ? $1 : "nada")."\n"; print "s2: \'$s2\', match=".($s2 =~ $pat ? $1 : "nada")."\n"; print "s3: \'$s3\', match=".($s3 =~ $pat ? $1 : "nada")."\n";
Updated: Removed half the test cases, since there's really only 3 possibilities. Also added fancy pattern that ensures either start/end of string or space is present.s1: 'alpha="first" beta="second" gamma="third"', match=beta="second" s2: 'alpha="first" gamma="third" beta="second"', match=beta="second" s3: 'beta="second" alpha="first" gamma="third"', match=beta="second"
In reply to Re: match whitespace or beginning/end of string
by gmargo
in thread match whitespace or beginning/end of string
by azadian
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |