regular expression to find the first occurance of a number from a given keyword

125 Views Asked by At

I have a string

"ZTFN00 identification number is 89320394 and mobile number is +918017828848"

and I want to identify the first occurrence of a number (89320394 in this case) from the keyword ZTFN. Also, the expression shall not return 00 with the ZTFN and only return the first occurrence of the number.

I tried \d+(?!ZTFN00) but it does not work !!

Please suggest

3

There are 3 best solutions below

11
Sobrique On

This should do it:

perl -ne '/ZTFN00\D+(\d+)/ && print $1,"\n"' yourfile

If your 'number' is always separated by whitespace ( where there 00 after ZTFN isn't) then you can use that as your test:

perl -ne 'print m/\b(\d+)\b/,"\n"' yourfile
0
Benjamin W. On
grep -Po 'ZTFN00.*?\K\b\d+\b'

Returns the first number that's a between word boundaries in a line where ZTFN00 is seen before that number. \K is a variable length look-behind: "match what's to the left of me, but don't retain it".

0
Abhik Ghosh On

Thanks guys ! In simple term, what I am looking for is this If there is a string, which has a keyword ZTFN00, then the regex shall be able to return the closest 9 to 11 digit number to the left or right side of the string.

I want to do this in REGEXP_REPLACE function of oracle.

Below are some of the sample strings

  1. The following error occurred in the SAP UPDATE_BP service as part of the combine (error:653, R11:186:Number 867278489 Already Exists for ID Type ZTFN00)

In the above, the regex shall return 867278489

  1. The following error occurred in the SAP UPDATE_BP service as part of the combine (error:653, R11:186:Number ZTFN00 identification number 123456778 already exist)

In the above, the regex shall return 123456778

Many thanks for sharing your expertise and guidance