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.
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.
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.
The array_unique() function removes duplicate values from an array.
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(/,/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With