in reply to Re^3: pattern matching
in thread pattern matching
Now I wish I could say you could handle this easily with a single pattern, but unfortunately, a repetition modifier around captures doesn't produce the desired results:
will only retain two captures: in the end, $1 will be 'de', the first capture, and $2 will be 'ef', the last one — the rest will simple have been forgotten about.$_ = 'de ad be ef #junk'; /^(\w\w)(?: (\w\w))*/;
There's no way around it, this requires a two step approach: Step 1) extract the whole of all the captures, Step 2), split it into parts.
$_ = 'de ad be ef #junk'; /^(\w\w(?: \w\w)*)/; my @capture = split ' ', $1;
$_ = 'de ad be ef #junk'; my @capture = /\G(?:^|\ )(\w\w)/g;
$_ = 'de ad be ef #junk'; my @capture; while(/\G(?:^|\ )(\w\w)/g) { push @capture, $1; }
Be extremely careful with the latter that you don't accidently cause an endless loop. I did, with
I'm still not sure why./(?:^|\G\ )(\w\w)/g
|
|---|