What regular expression can I use to find the entry Nα΅—Κ° in a comma-separated list?

I need a regular expression that can be used to search for the entry N th in a comma-separated list.

For example, let's say that this list looks like this:

abc,def,4322, mail@mailinator.com ,3321,alpha-beta,43 

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

+6
source share
3 answers

My first thought was not to use a regular expression, but to use something that separates the string into an array with a comma, but since you requested the regular expression.

most regular expressions allow you to specify a minimum or maximum match, so something like this is possible.

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

This is intended to match any number of characters that are not a comma, followed by a comma six times exactly (?:[^,]*,){6} - ?: Says it cannot be written - and then for matching and writing any number of characters that are not a comma ([^,]+) . You want to use the first capture group.

Let me know if you need more information.

EDIT: I edited above so as not to capture the first part of the line. This regular expression works in C # and Ruby.

+7
source

You can use something like:

 ([^,]*,){$m}([^,]*), 

As a starting point. (Replace $ m with (n-1).) The content will be in capture group 2. This does not handle things like lists of size n, but it is only a matter of making the appropriate changes for your situation.

0
source
 @list = split /,/ => $string; $it = $list[6]; 

or simply

 $it = (split /,/ => $string)[6]; 

A bit that writes a pattern with {6} in it every time.

-2
source

Source: https://habr.com/ru/post/910818/


All Articles