|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Thu Dec 29, 2005 6:19 pm
Parsing Bug |
I've come across the following bug when parsing regular expressions.
The following commands should all match
Code: |
#SHOW %match("5 to 9 (average 7)", "(%d) to (%d) ~(average (%d)~)")
#SHOW %match("5 to 9 (average 7)", "(%d) to (%d) \(average (%d)\)")
#SHOW %regex("5 to 9 (average 7)", "(\d+) to (\d+) \(average (\d+)\)") |
but only the third one does.
Has anyone else seen this?
Here's one for you to tackle in CMUD Zugg. |
|
_________________ Asati di tempari! |
|
|
|
nexela Wizard
Joined: 15 Jan 2002 Posts: 1644 Location: USA
|
Posted: Thu Dec 29, 2005 6:31 pm |
#2 won't work cause \ is the regex escape char
as for #1 Iit is using the tildes to treat the next char as a literal and then consuming the tilde :p
ie it is trying to perform a match against
#SHOW %match("5 to 9 (average 7)", "(5) to (9) (average (7))")
which wont match cause it is using the parenthesis around average to capture to a var
%1=5
%2=9
%3=average 7
%4=7
Workaround keep the tilde by using two
#SHOW %match("5 to 9 (average 7)", "(%d) to (%d) ~~(average (%d)~~)") |
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Thu Dec 29, 2005 8:38 pm |
Hey Nexela,
I think I should explained more. I did the one with '\' just to dispel what i thought would get about trying that instead of the tilde. So #2 was really just to stop folks from looking at #1 and saying try '\' instead of the '~'..
As for #1 the pattern I want to match is "5 to 9 (average 7)" and you should notice that the tilde's are only where highlighted... i.e. "(%d) to (%d) ~(average (%d)~)".
So while I'm expecting
%1=5
%2=9
%3=7
which I do get with #3 that's not what I get with #1 because it doesn't show as matching at all.
To be clear the pattern I want to match is 'X to Y (average Z)' where X, Y, and Z are numbers. #1 should match but it doesn't and I'm convinced that it's the parantheses that are throwing off the parsing. I also tried
Code: |
#SHOW %match("5 to 9 average 7", "(%d) to (%d) average (%d)")
|
and that worked as expected. |
|
_________________ Asati di tempari! |
|
|
|
nexela Wizard
Joined: 15 Jan 2002 Posts: 1644 Location: USA
|
Posted: Thu Dec 29, 2005 9:00 pm |
The workaround explaned it, use 2 tildes instead of 1
#SHOW %match("5 to 9 (average 7)", "(%d) to (%d) ~~(average (%d)~~)")
Becomes
#SHOW %match("5 to 9 (average 7)", "(5) to (9) ~(average (7)~)")
1
%1=5
%2=9
%3=7 |
|
|
|
Tech GURU
Joined: 18 Oct 2000 Posts: 2733 Location: Atlanta, USA
|
Posted: Fri Dec 30, 2005 12:04 am |
Ok... I think I get it now. Not quite sure I agree with the parsing.. but if it keeps all my cool parsing tricks I'll deal with it. :)
That's for the explanation. I totally missed (and I've actually written several parsers and simple compilers.) |
|
_________________ Asati di tempari! |
|
|
|
|
|