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:

#!/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";
Produces:
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"
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.


In reply to Re: match whitespace or beginning/end of string by gmargo
in thread match whitespace or beginning/end of string by azadian

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.