Can't extract two fields out of some text using regex

Viewed 38

I'm trying to fetch three fields from some text using regex. The value of fields that I'm after are PLAN,TITLE and REFERENCE. I can extract the value of REFERENCE but I'm stuck with the other two fields. How can I get the value of PLAN and TITLE using regex?

I've tried with (it gives me the reference number):

import re

item_str = """
S
PLAN             SHORT                                         TITLE
0030 278 592     0420091;16                                    052 400 228

REFERENCE: 042 420 626
"""
ref = re.findall(r"REFERENCE:\s+(.*)",item_str)[0]
print(ref)
1 Answers

You can try this example (Regex101):

import re

item_str = """
S
PLAN             SHORT                                         TITLE
0030 278 592     0420091;16                                    052 400 228

REFERENCE: 042 420 626
"""

print( re.findall(r'(?=^PLAN).*?(^[\d ]+?)\s{2,}.*\s{2,}(\d.*?$).*?^REFERENCE:\s*([\d ]+)', item_str, flags=re.M|re.S) )

Prints:

[('0030 278 592', '052 400 228', '042 420 626')]
Related