Search code examples
regexregex-groupregexp-replace

REGEX to cover just first match from all


I have this type of regex

\b[0-9][0-9][0-9][0-9][0-9]\b

It's not complete, this will match me many examples of 5 digit but I need just first and one match from this structure:

Reference Number            WW  
30966                     CFUN22       098765334    
30967                     CFUN22       098765335

30968                     CFUN22       098765336        
30969                     CFUN22    098765337

In this case I need just "30966" , not 30967,30968 and so on...

I tried to do

\b[0-9][0-9][0-9][0-9][0-9]\b

Solution

  • You can use a positive lookbehind to make sure that you're grabbing the first 5-digit number after the word "Comments":

    (?<=Comments\n)\d{5}\b
    

    https://regex101.com/r/pZLj4K/1