Home > Back-end >  VB.net regex find value between second commas
VB.net regex find value between second commas

Time:01-02

I am trying to use Regex to pull the value from between the second set of commas. That has the letter S in the in beginning word.

(?<=S,[^,],)[^,] (?=,)

Above is the closest I can get. It will give the value I am looking for on the BS and CS lines. I was thinking the [^'] in the look behind would get(exclude) all the characters between the first set of commas.

BB,21,1.750000,0.000000,
AS,21.5,3.250000,-0.187500
BS,21,3.250000,-0.187500
CS, ,1.750000,-0.375000
DS,1,30.375000,-0.375000
QA,aa,30.375000,-0.375000

Looking for more than just the answer would really like an explanation so I can learn from it.

CodePudding user response:

You can repeat the character class in the lookbehind at the beginning, but in this case "value from between the second set of commas" is literally 2 comma's to the left of the current value that you are matching.

The S is the last value before the comma, and it can have multiple matches as there are no anchors.

(?<=S,[^,]*,)[^,] (?=,)

Regex101 demo

If the lines start with only uppercase chars A-Z, you could match at least a single S char in that first part

(?<=^[A-RT-Z]*S[A-Z]*,[^,\n]*,)[^,\n] (?=,)

The pattern matches:

  • (?<= Positive lookbehind, assert that to the left is
    • ^ Start of string
    • [A-RT-Z]* Match optional chars A-Z without S
    • S[A-Z]*Match S and optional chars A-Z
    • ,[^,\n]*, Match 2 times a comma
  • ) Close the lookbehing
  • [^,\n] Match 1 chars other than a comma or newline
  • (?=,) Assert a comma to the right

Regex101 demo

  • Related