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.


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?



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.


source to share

2 answers

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



Watch the demo


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



Watch the demo



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



Online test here .



All Articles