How to capture a particular delimited  field using REGEX

Richard Gouette
Richard Gouette used Ask the Experts™
on
Need to capture the "3" value using REGEX in the following  string:

"0338829990|10881|188892|ELRET|","","3","","2018-08-23T09:55:23-04:00"
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
Does this work for you?
.*"(\d)"

Open in new window

Group1 matches with 3.

Or just this?
"(\d)"

Open in new window

Richard GouetteIT Manager

Author

Commented:
It seems to work.

since the "3" field could be any number, I found that "(\d+)" seems to work.
I know \d matches "any digit"...
but, could you explain a little of why this simple regex  works?

Thanks much
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
I assumed there could be only one digit so I used just \d but \d+ will work if there could be more than one digit.

There is nothing fancy to explain...
.*"(\d)"
.* matches any character (except for new line)
* Quantifier — Matches between zero and unlimited times, as many times as possible.
" matches the character " literally

1st Capturing Group (\d) matches with 3

The second one is simple and should also work i.e. "(\d+)" as it will match any number of digits surrounded by double quotes.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Richard GouetteIT Manager

Author

Commented:
I actually, didn't properly understand groups and their usage.
thank you
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome Richard!
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
I think you forgot to award the bonus points. :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial