in reply to match whitespace or beginning/end of string
Note that the $pre and $post patterns can be adjusted to include as much or as little of the surrounding context (i.e., the spaces) as desired.>perl -wMstrict -le "my $pre = qr{ \A | \s }xms; my $post = qr{ \s | \z }xms; my $str = q{foo='123' barfoo='987' bar='555'}; for my $user_supplied (@ARGV) { my $rx = qr{ $pre \Q$user_supplied\E $post }xms; $str =~ m{ ($rx) }xms; print qq{[$user_supplied] matches }, $1 ? qq{[$1]} : q{nothing}; } " foo='123' bar='555' barfoo='987' foo='987' [foo='123'] matches [foo='123' ] [bar='555'] matches [ bar='555'] [barfoo='987'] matches [ barfoo='987' ] [foo='987'] matches nothing
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: match whitespace or beginning/end of string
by azadian (Sexton) on Nov 02, 2009 at 14:56 UTC | |
by AnomalousMonk (Archbishop) on Nov 02, 2009 at 18:52 UTC |