www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - "repeating pair" regex

reply "Thor" <Thor hotmail.com> writes:
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?
May 21 2012
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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`)) { //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
May 21 2012
parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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
May 21 2012
parent "Thor" <Thor hotmail.com> writes:
On Monday, 21 May 2012 at 13:06:45 UTC, Dmitry Olshansky wrote:
 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.
Great, thanks for your quick support. :) I actually did test `gx` but I failed to realise that I now have to iterate over matches as well as captures... which makes perfect sense when I pause to think about it.
May 21 2012