John S
John S

Reputation: 8341

Regular expression to grab a specific section of sample

I am trying to find/match the number sequences in bold in the example lines below using regular expressions.

2520222 Rubble,Barney 1134525

1524356 Mudd,Harry S 14-40525 111.11

2324316 Mudd,Mary R 12-40000

The closest that I've come is

\d{0,2}-?\d+$

But that only works when the section I want to capture is at the end of the line because of the $. If there is an unwanted item at the end of the line (see line #3) it will always be a decimal. Is there a way find that and step back one group to the correct sequence? Is it even possible in RegEx?

Upvotes: 2

Views: 43

Answers (1)

Federico Piazza
Federico Piazza

Reputation: 30995

You can use look arounds in a regex like this:

(?<= )[\d-]+(?=\s|$)

Working demo

enter image description here

If you don't want to use lookarounds, then you could use a regex like this:

,.*\s([\d-]+)(?:$|\s)

Working demo

Upvotes: 5

Related Questions