in reply to dumb regex question
ikegami's solution will work nicely if you only have one such pattern per line. If you have more than one such pattern, you might want to try using the g modifier with your regex. This lets you repeated search for a pattern in a line, like this:
use strict; use warnings; my $FORMAT="%-48s match=%s\n"; while (my $line = <DATA>) { chomp $line; my $bFound=0; while ($line =~ m{"/([^"]+)"|/(\S+)}g) { my $sMatch = defined($1) ? $1 : $2; print sprintf($FORMAT, "<$line>", "<$sMatch>"); $bFound=1; } print sprintf($FORMAT, "<$line>", "---") unless $bFound; } __DATA__ "/moreIters 10" "/blah X Y Z" /fewIter /some stuff here "/albatross" foo bar baz monkeys leprechauns /not monkeys /gnomes "not leprechauns though" /grumpy gnomes "/silly elves" "/funny" fairies
which outputs
<"/moreIters 10"> match=<moreIters 10> <"/blah X Y Z"> match=<blah X Y Z> </fewIter> match=<fewIter> </some stuff here> match=<some> <"/albatross" foo bar baz> match=<albatross> <monkeys> match=--- <leprechauns /not monkeys> match=<not> </gnomes "not leprechauns though"> match=<gnomes> </grumpy gnomes "/silly elves" "/funny" fairies> match=<grumpy> </grumpy gnomes "/silly elves" "/funny" fairies> match=<silly elves> </grumpy gnomes "/silly elves" "/funny" fairies> match=<funny>
Best, beth
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: dumb regex question
by linuxfan (Beadle) on Apr 07, 2009 at 16:46 UTC |