in reply to match whitespace or beginning/end of string
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"
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: match whitespace or beginning/end of string
by azadian (Sexton) on Oct 30, 2009 at 13:17 UTC | |
by gmargo (Hermit) on Oct 30, 2009 at 13:19 UTC | |
by azadian (Sexton) on Oct 30, 2009 at 14:04 UTC | |
by ack (Deacon) on Oct 30, 2009 at 16:06 UTC | |
by azadian (Sexton) on Nov 02, 2009 at 15:16 UTC |