-1

I want to capture the third comma in strings like:

98,52,"110,18479456000019"

I thought of something like a character except:

[^"0123456789]

But, result was the capture of all commas.

After that, I've tried some regex about nth capture - seems to be a solution -, but none works.

How do I solve this problem?

Emma
  • 27,428
  • 11
  • 44
  • 69

1 Answers1

1

There are several ways to capture the third ,. This RegEx is one way to do so:

([\d,])\x22\d+(,)\d+\x22

where your desired , is in the second group (,), just to be simple, and you can call it using $2.

I have added additional boundaries to this RegEx for safety, which you can remove it:

enter image description here

\x22 is just ", which you can replace, if you wish:

([\d,])"\d+(,)\d+"

You can also use (\) and escape a char, where necessary.


If your input would be a bit more complex, maybe such as this:

enter image description here

you might create a middle boundary before the third , and add all possible chars in the middle boundary ([\d\w\"]+), such as this RegEx:

 (\d+,){2}[\d\w\"]+(,)

and capture the third , using $2. This time you can also relax your expression from the right side, and it would still work.

You might also add a start ^ in the regex:

^(\d+,){2}[\d\w\"]+(,)

as an additional left boundary which means your input must start with this expression.

Emma
  • 27,428
  • 11
  • 44
  • 69