Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comma-separated repeats

Tags:

regex

I've got a pretty long regex to match an entry in a list I'm processing. The list should be one or more of these entries, comma-separated. Consider a regex:

([abc]+|[123]+)

for the entry. To match my comma-separated list, I'm matching against something like this:

([abc]+|[123]+)(,([abc]+|[123]+))*

(It looks especially foolish with my nasty regex instead of the short one I used here for the example)

I feel there must be a better way than having two copies of the entry -- once for the first entry, again for and follow comma/entry pairs.

like image 643
ajwood Avatar asked Mar 08 '11 15:03

ajwood


People also ask

How do you remove duplicates from a comma separated string?

We can remove duplicates from a string in the following three steps: Convert comma separated string to an array; Use array_unique() to remove duplicates; Convert the array back to a comma separated string.

What is a comma separated list example?

For example, C{:,1} is a comma-separated list if C has more than one row. A comma-separated list is produced for a structure array when you access one field from multiple structure elements at a time.

How to remove duplicate string value in php?

The array_unique() function removes duplicate values from an array.


1 Answers

Looks like you want backreferences.

([abc123])(,\1)*

Also, just FYI, [abc]|[123] is equivalent to [abc123].


Edit: Based on your edit, I think I misunderstood what you were trying to do. Try this:

([abc123]+(,|$))*

Or if you want to be less restrictive:

([^,]+(,|$))*

This matches strings of non-comma characters separated by commas. A simpler approach would just be a global match for [^,]+ by itself. In JavaScript, that would look like this:

myString.match(/[^,]+/g) //or /[abc123]+/g, or whatever

Or you could just split on commas:

myString.split(/,/)
like image 64
Justin Morgan Avatar answered Sep 20 '22 03:09

Justin Morgan