digitalmars.D.learn - "repeating pair" regex
- Thor (11/11) May 21 2012 Hi,
- Dmitry Olshansky (10/19) May 21 2012 Yes, just use the global flag and drop ( )+.
- Dmitry Olshansky (5/27) May 21 2012 Bleh. Fixed:
- Thor (5/36) May 21 2012 Great, thanks for your quick support. :)
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
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
On 21.05.2012 17:05, Dmitry Olshansky wrote:On 21.05.2012 16:42, Thor wrote:Bleh. Fixed: foreach( m; match(..., regex(`\s* (\w+)=(\d+) `, `gx`)))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
On Monday, 21 May 2012 at 13:06:45 UTC, Dmitry Olshansky wrote:On 21.05.2012 17:05, Dmitry Olshansky wrote: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.On 21.05.2012 16:42, Thor wrote:Bleh. Fixed: foreach( m; match(..., regex(`\s* (\w+)=(\d+) `, `gx`)))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.
May 21 2012