On Jan 13, 5:41 pm, Junkone [email protected] wrote:
The string is as follows and the tokens are equavalent to
irb(main):047:0> a =~(“Add comp i pe”)
I dont get match at 4th token
There are a few problems here, I think.
As written, the second token can’t have whitespace. Is that what you
You want ([isf]) for your third token. That will match a single
instance of one of the three characters.
Also, I’m not clear on what you want for the fourth token. Do you want
0 or 1 of [pei] or 0 or 1 of each of [pei] - which of these are
valid for the fourth token: e or pe.
You should also end the regex with \s* unless there’s always going to
be whitespace. You’re currently checking for one or more whitespace
characters at the end, but your test didn’t have any.
Assuming you do allow 0 or 1 of each of [pei] for the fourth token and
0 or more whitespace characters at the end, I would use:
r = /(add)\s+(\S+)\s+([isf])\s+(p?e?i?)\s*/i
Though, if there can’t be whitespace in any of the tokens, you might
want to use split rather than a regex. The regex above can only get
the fourth token right if the letters are in order - p will match, ei
will match, ip will not. Split wouldn’t have that problem. There may
be a regex way around that, but I can’t think of it at the moment.
“Add comp i ep”.scan r # => [[“Add”, “comp”, “i”, “e”]]
“Add comp i ep”.split # => [“Add”, “comp”, “i”, “ep”]