Home > OS >  Find the specific number using regex python near particular word
Find the specific number using regex python near particular word

Time:10-01

I have array of patterns like keywords

patterns = [reference number, Ref:, Invoice:, Sales Quote No:]

Sentence 1: My document reference number 25XPOI9876 which is entered
Sentence 2: Sales Quote No: SP21-SQ10452 entered quote number
Sentence 3: Ref:9874621kl is attached:

Is there any solution to get the the output:

  1. 25XPOI9876
  2. SP21-SQ10452
  3. 9874621kl

CodePudding user response:

Try this Regex,

Captures only 25XPOI9876, SP21-SQ10452, 9874621kl

(?:(?<=reference number )|(?<=Ref:)|(?<=Invoice:)|(?<=Sales Quote No: ))[\w\d-]

Output:

image

Regex101 Demo

Sample code in Python:

import re

a = '''Sentence 1: My document reference number 25XPOI9876 which is entered
Sentence 2: Sales Quote No: SP21-SQ10452 entered quote number
Sentence 3: Ref:9874621kl is attached:'''

print(re.findall(r'(?:(?<=reference number )|(?<=Ref:)|(?<=Invoice:)|(?<=Sales Quote No: ))[\w\d-] ', a))

Outputs:

['25XPOI9876', 'SP21-SQ10452', '9874621kl']

tell me if its okay for you...

  • Related