Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What regular expression can I use to find the Nᵗʰ entry in a comma-separated list?

I need a regular expression that can be used to find the Nth entry in a comma-separated list.

For example, say this list looks like this:

abc,def,4322,[email protected],3321,alpha-beta,43

...and I wanted to find the value of the 7th entry (alpha-beta).

like image 469
Jared Avatar asked Mar 15 '12 15:03

Jared


People also ask

How do you match a comma in RegEx?

The 0-9 indicates characters 0 through 9, the comma , indicates comma, and the semicolon indicates a ; . The closing ] indicates the end of the character set. The plus + indicates that one or more of the "previous item" must be present.

What is ?: In RegEx?

It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.

How do you match a semicolon in RegEx?

Semicolon is not in RegEx standard escape characters. It can be used normally in regular expressions, but it has a different function in HES so it cannot be used in expressions. As a workaround, use the regular expression standard of ASCII.

Is comma special character in RegEx?

In regex, there are basically two types of characters: Regular characters, or literal characters, which means that the character is what it looks like. The letter "a" is simply the letter "a". A comma "," is simply a comma and has no special meaning.


1 Answers

My first thought would not be to use a regular expression, but to use something that splits the string into an array on the comma, but since you asked for a regex.

most regexes allow you to specify a minimum or maximum match, so something like this would probably work.

/(?:[^\,]*,){5}([^,]*)/

This is intended to match any number of character that are not a comma followed by a comma six times exactly (?:[^,]*,){5} - the ?: says to not capture - and then to match and capture any number of characters that are not a comma ([^,]+). You want to use the first capture group.

Let me know if you need more info.

EDIT: I edited the above to not capture the first part of the string. This regex works in C# and Ruby.

like image 96
gymbrall Avatar answered Sep 22 '22 20:09

gymbrall