I have the following data
Animals = Dog Cat Turtle \
Mouse Parrot \
Snake
I would li开发者_开发百科ke the regex to construct a match of just the animals with none of the backslashes: Dog Cat Turtle Mouse Parrot Snake
I've got a regex, but need some help finishing it off.
/ANIMALS\s*=\s*([^\\\n]*)/
Since you specified a language, I need to ask you this: Why are you relying on the regex for everything? Don't make the problem harder than it has to be by forcing the regex to do everything.
Try this approach instead...
- Use
gsub!
to get rid of the backslashes. split
the string on any whitespace.- Shift out the first two tokens ("Animals", "=").
- Join the array with a single space, or whatever other delimiter.
So the only regex you might need is one for the whitespace delimiter split. I don't know Ruby well enough to say exactly how you would do that, though.
How 'bout the regex \b(?!Animals\b)\w+\b
which matches all words that aren't Animals
? Use the scan
method to collect all such matches, e.g.
matchArray = sourceString.scan(/\b(?!Animals\b)\w+\b/)
Make sure you are matching with ignore-case, because ANIMALS will not match Animals without it.
精彩评论