It’s been a while since I’ve done any serious regular expressions and It’s taking me ages to do something that should be quite simple.
It’s quite simple in that I’m delimiting the action and arguments.
I only need four named capture groups, ACTION, FROM, TO or FOUR.
I just need extract arguments from a string, these are some example of the string I’m trying to match against.
Input string: someaction from today until tomorrow
Captures: (ACTION=someaction, FROM=’today’, UNTIL=’tomorrow’, FOR=null)
Input String: someaction now + 3 until 12/12
Captures: (ACTION=someaction, FROM=’now + 3′, UNTIL=’12/12′, FOR=null)
Input string: someaction from tomorrow for 2 days
Captures: (ACTION=someaction, FROM=’tomorrow’, UNTIL=null, FOR=’2 days’)
Input string: someaction today for 6 hours
Captures: (ACTION=someaction, FROM=’today’, UNTIL=null, FOR=’6 hours’)
ACTION will always be the first whitespace delimited string. ‘someaction’ from my example.
Next will be either the word “from” or the value for “from” delimited by the words “until” or “for”.
Then will always follow the words, “until” or “for”.
Finally the named capture for “until” or “for” for the rest of the string.
I haven’t posted what I’ve got as it’s a complete mess. It doesn’t feel complicated what I’m trying to do, can anyone help give me some pointers.
Thanks,
Sam
As you don’t say what language you’re working with, here is a perl script that does the job:
output:
It should be converted easily to another language.
explanation: