Capturing an optional second group of digits (not repeating) using a regular expression

I have a huge dataset where I am trying to extract a group of 4 digits. The problem is that sometimes there will be a 4-digit preceding group which I don't want. These two groups will never be the same with each other.

Example:

String String 7777 Some more string  
String 1234 7777 Some more string

      

In both cases, I want to extract ONLY 7777

(or any combination of numbers replaces it). There is no picture to tell which group of numbers will be in which position - any number from 0000 to 9999 can be in the first or second position.

If it were possible, I think he will do what I want?

\b\d{4}{0,1}\s{0,1}(\d{4})\b

      

Extra 4 digits, extra space, 4 digits. But I tried this and some variations of it, but I cannot get it to work!

Looking ahead seems like a likely candidate, but I don't understand how to build a template.

+3


source to share


2 answers


You can use negative look and feel to check if there is a subsequent 4-digit number after it:

\b\d{4}\b(?!\s?\d{4}\b)

      

Watch the demo

EDIT:



To write down a 4-digit number that is not followed by text and another 4-digit number, you must use:

\b\d{4}\b(?!.+\b\d{4}\b)

      

Watch the demo

+2


source


You can use this expression, which matches a four-digit group, not followed by any other four-digit groups:

\d{4}(?!.+\d{4}.+)

      



Online test here .

+2


source







All Articles