Home > Blockchain >  REGEX to cover just first match from all
REGEX to cover just first match from all

Time:01-20

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

CodePudding user response:

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

CodePudding user response:

Try using the following regex:

^\N \n.*?(\d{5})

It will match:

  • ^: start of string
  • \N \n: any sequence of non-newline characters, followed by newline
  • \n: the newline character
  • .*?: optional smallest sequence of characters
  • (\d{5}): "Group 1" - sequence of five characters

Your needed digits can be found within Group 1.

Given you're dealing with a textual table, using \N\n will allow you to skip the header from selection, while .*? will allow to match your code not necessarily at the beginning of the second line.

Check the regex demo here.

  • Related