Get Value Between 2Nd and 3Rd Comma

Get value between 2nd and 3rd comma

select 
regexp_substr('2901 MAIN ST,CORNING,NY,14830', '(.*?,){2}(.*?),', 1, 1, '', 2)
from dual

In general,

n_th_component := 
regexp_substr(string, '(.*?,){'||(n-1)||'}([^,]*)', 1, 1, '', 2);

Example:

select 
n,
regexp_substr('2901 MAIN ST,CORNING,NY,14830',
'(.*?,){'||(n-1)||'}([^,]*)', 1, 1, '', 2)
from (select level n from dual connect by level <= 4)

Extract words between the 2nd and the 3rd comma

data = "Chateau d'Arsac, Bordeaux blanc, Cuvee Celine, 2012"
import re
print re.match(".*?,.*?,\s*(.*?),.*", data).group(1)

Output

Cuvee Celine

But for this simple task, you can simply split the strings based on , like this

data.split(",")[2].strip()

Regex - How can I take everything between the second and third comma?

You don't need regex for this task, using explode will do the job, e.g.

$myArray = explode(",", "4w4190,UK,value,,another data,,job,1,,23");
var_export($myArray);

Returns

array (
0 => '4w4190',
1 => 'UK',
2 => 'value',
3 => '',
4 => 'another data',
5 => '',
6 => 'job',
7 => '1',
8 => '',
9 => '23',
)

Where index 2 contains your value ($myArray[2]).

Ruby: extract substring between 2nd and 3rd fullstops

list = my_string.split(".")
list[2]

That will do it I think. First command splits it into a list. Second gets the bit you want

Split string by second comma, then by third comma

If your string looks exactly as you pasted here, and it will always have such pattern, then you can split on a string of two characters:

items[1].Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);

Update:

But if your string is without spaces, then you should replace first with some unique character like |, and then split over that new character:

items[1].Replace(",$", "|$").Split(new char[1] { '|' }, StringSplitOptions.RemoveEmptyEntries);

Also you can replace it with or $ and the string would be already in desired format, and there would be no need to split and join again.

This should do the trick.

RegEx - How to select the second comma and everything after it

You may use a Perl regex option with the following pattern:

^([^,]*,[^,]*),.*

and replace with \1.

See the regex demo.

Details:

  • ^ - start of string
  • ([^,]*,[^,]*) - Group 1 (later referred to with \1 backreference from the replacement pattern):

    • [^,]* - any 0+ chars other than a comma (to prevent overflowing across lines, add \n\r into the negated character class - [^,\n\r]*)
    • , - a comma
    • [^,]* - any 0+ chars other than a comma
  • , - a comma
  • .* - any 0+ chars other than line break chars as many as possible


Related Topics



Leave a reply



Submit