Reputation: 15902
I have the following string:
type:crash status:new "status:in progress 2" type:bug email:[email protected]
I want it to be matched to:
Match #1
type:crash
Match #2
status:new
Match #3
status:in progress 2
Match #4
type:bug
Match #5
email:[email protected]
Here is what I tried:
(\w+:\w+|"\w+:[\w\s]+)
Unfortunately I couldn't match to the double quotes nor the email parts, can you please tell me how to enhance this regex to match to the above data.
Upvotes: 1
Views: 60
Reputation: 106077
Don't use a regex. Use shellsplit
from Shellwords in the standard library. It's designed for this exact thing and covers the edge cases you'll have a very had time covering with a regex.
require 'shellwords'
str = 'type:crash status:new "status:in progress 2" type:bug email:[email protected]'
p str.shellsplit
# => [ "type:crash",
# "status:new",
# "status:in progress 2",
# "type:bug",
# "email:[email protected]" ]
Upvotes: 1
Reputation:
Not sure about specific edge cases, but this regex should match everything in your example. I tested it on Rubular.
("[^"]+"|[^\s]+:[^\s]+)
Basically "anything in quotes" or "anything not a whitespace char one or more times followed by a colon and then followed by something not a white space char"
Upvotes: 1