"repeating pair" regex
Dmitry Olshansky
dmitry.olsh at gmail.com
Mon May 21 06:06:43 PDT 2012
On 21.05.2012 17:05, Dmitry Olshansky wrote:
> On 21.05.2012 16:42, Thor wrote:
>> Hi,
>>
>> I tried something like this...
>> auto m = match("a=42 b=32 c=22", regex(`^(?: \s* (\w+)=(\d+) )+$`, `x`));
>>
>> This works fine...
>> `^(?: \s* (\w+)=(\d+) )+$`
>> ... and if I duplicate the string x times, it also works...
>>
>> But I'm hoping to parse a variable number of these 'pairs'... so I tried
>> grouping the pairs in (?:)+ but this way it saves only the last pair?
>>
>> Anyone got an idea?
>>
> Yes, just use the global flag and drop ( )+.
>
> Then this foreach ought to save the day:
> foreach( m; match(..., `\s* (\w+)=(\d+) `, `gx`))
Bleh. Fixed:
foreach( m; match(..., regex(`\s* (\w+)=(\d+) `, `gx`)))
> {
> //some stuff with m[1] & m[2]
> }
>
> Throw in some experiment w.r.t. the end of line and you should be fine.
>
--
Dmitry Olshansky
More information about the Digitalmars-d-learn
mailing list