alasarr
alasarr

Reputation: 1605

Python regex extract string multiline

From this string:

s = 'OBS VA DTG:           07/1200Z\r\r\nEST VA CLD:           SFC/FL200 S2115 W17500 - S2015 W17000 - S2415\r\r\n                      W16930 - S2300 W17815 - S2030 W17615 - S2030\r\r\n                      W17515 - S2115 W17500 FL200/600 NO VA EXP\r\r\nFCST VA CLD+6 HR:     07/1800Z SFC/FL600 NO VA EXP\r\r\nFCST VA CLD+12 HR:    08/0000Z SFC/FL600 NO VA EXP\r\r\nFCST VA CLD+18 HR:    08/0600Z SFC/FL600 NO VA EXP\r\r\n'

Pretty printed is:

OBS VA DTG:           07/1200Z
EST VA CLD:           SFC/FL200 S2115 W17500 - S2015 W17000 - S2415
                      W16930 - S2300 W17815 - S2030 W17615 - S2030
                      W17515 - S2115 W17500 FL200/600 NO VA EXP
FCST VA CLD+6 HR:     07/1800Z SFC/FL600 NO VA EXP
FCST VA CLD+12 HR:    08/0000Z SFC/FL600 NO VA EXP
FCST VA CLD+18 HR:    08/0600Z SFC/FL600 NO VA EXP

I'd like a regular expression to extract the EST VA CLD value. That should be the output:

SFC/FL200 S2115 W17500 - S2015 W17000 - S2415
W16930 - S2300 W17815 - S2030 W17615 - S2030
W17515 - S2115 W17500 FL200/600 NO VA EXP

I've tried:

>>> match = re.search(r"EST VA CLD:(.+)\n.+:",s,re.DOTALL)
>>> print match.group(1)

Upvotes: 1

Views: 162

Answers (2)

vks
vks

Reputation: 67988

EST VA CLD:\s*([\s\S]+?)\n(?=[^:\n]*:)

Try this.Grab the capture.See demo.

https://regex101.com/r/pM9yO9/22#python

Upvotes: 2

GLHF
GLHF

Reputation: 4054

How about;

s = 'OBS VA DTG:           07/1200Z\r\r\nEST VA CLD:           SFC/FL200 S2115 W17500 - S2015 W17000 - S2415\r\r\n                      W16930 - S2300 W17815 - S2030 W17615 - S2030\r\r\n                      W17515 - S2115 W17500 FL200/600 NO VA EXP\r\r\nFCST VA CLD+6 HR:     07/1800Z SFC/FL600 NO VA EXP\r\r\nFCST VA CLD+12 HR:    08/0000Z SFC/FL600 NO VA EXP\r\r\nFCST VA CLD+18 HR:    08/0600Z SFC/FL600 NO VA EXP\r\r\n'
t = s.split()

print (" ".join(t[t.index("CLD:")+1:t.index("FCST")]))

Upvotes: 0

Related Questions