AJFaraday
AJFaraday

Reputation: 2450

Ruby Regex - matching a list in text

So my goal is simple, parse a message with a list, such as these:

show tom and harry
show tom, dick and harry
show tom, dick, jane and harry

My problem is that I can write a regex which matches a list with or without the middle elements...

/show (\w+)[, (\w+)]* and (\w+)/

In this case it appears to match tom and harry, but dick and jane are not returned as arguments.

Example (from rubulus)

regex matching missing middle elements of list

It appears the round brackets within the squared brackets are ignored. Does anyone know how I can match these?

Upvotes: 2

Views: 203

Answers (3)

nu11p01n73R
nu11p01n73R

Reputation: 26667

How about something like

(?:(?<=and )|(?<=, )|(?<=show ))(\w+)

Regex Demo Link

OR

(?<=and |, |show )\w+

Upvotes: 1

shivam
shivam

Reputation: 16506

Yet another way to do it:

a = "show tom, dick, jane and harry"
arr = a.split(/ |, /)
# => ["show", "tom", "dick", "jane", "and", "harry"]
arr.reject! {|item| item =~ /show|and/i }
# => ["tom", "dick", "jane", "harry"]

or

a = "show tom, dick, harry, alpha, beta, tango and charlie"
arr = a.split(/ |, /)
arr.reject! {|item| item =~ /show|and/i }
# => ["tom", "dick", "harry", "alpha", "beta", "tango", "charlie"]

Upvotes: 1

user4061059
user4061059

Reputation:

I doubt this is the most elegant solution, but it should work (as I've only tested it with regex101.com):

show (?'first'\w+)(?'comma'(, \w+)*)(?: and (?'and'\w+))?

Then you'll get an object with a field with the name of 'first', and possibly one with 'comma' and one with 'and'.

To extract all names in the 'comma' group, you have split the string on ', ' (comma-space) (the first member of the array will be empty)

I hope you will find this useful!

Upvotes: 0

Related Questions