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:
- 25XPOI9876
- SP21-SQ10452
- 9874621kl
CodePudding user response:
Try this Regex,
Captures only 25XPOI9876
, SP21-SQ10452
, 9874621kl
(?:(?<=reference number )|(?<=Ref:)|(?<=Invoice:)|(?<=Sales Quote No: ))[\w\d-]
Output:
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...