Re: Regex repeating capture
- From: Mirco Wahab <wahab-mail@xxxxxx>
- Date: Tue, 30 Jan 2007 18:59:37 +0100
Jay wrote:
I'm trying to break an input string into multpile pieces using a series of delimiters that start with an asterisk. Following the asterisk is a mulitple character identifier immediately followed by a
data string of variable length. Here is a simple example:
*CZ1 2.3 4-56 *fuuuS24364 08 23 72
I'd like to break this into
CZ
1 2.3 4-56
fuuu
S24364 08 23 72
I have tried the pattern (?:\*(CZ|fuuu)(.*)), which produces the
following ouput:
CZ
1 2.3 4-56 *fuuuS24364 08 23 72
How can I force it to repeat the capturing?
You can force the repeated capturing by the /g flag
on the regex.
Your complete solution should look, if I
guessed correct from your riddle, sth. like:
...
my $simple = q{*CZ1 2.3 4-56 *fuuuS24364 08 23 72 *AAA3 44 5-66};
my %hits;
$hits{$1} = $2 while $simple=~/\*([a-z]+|[A-Z]+)([^*\\z]+)/g;
print "$_ ==> $hits{$_}\n" for keys %hits;
...
This would print (on the above data):
CZ ==> 1 2.3 4-56
AAA ==> 3 44 5-66
fuuu ==> S24364 08 23 72
But your problem is not really completely specified ...
Regards
M.
.
- References:
- Regex repeating capture
- From: Jay
- Regex repeating capture
- Prev by Date: Re: Regex repeating capture
- Next by Date: Win32::OLE version conflict problem...please help
- Previous by thread: Re: Regex repeating capture
- Next by thread: Win32::OLE version conflict problem...please help
- Index(es):