Wazery
Wazery

Reputation: 15902

GitHub like search regex

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

Answers (2)

Jordan Running
Jordan Running

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

user2536065
user2536065

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

Related Questions