1

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)
0

2 Answers 2

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

Try this.Grab the capture.See demo.

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

Sign up to request clarification or add additional context in comments.

4 Comments

Could you explain what mean [\s\S]+??
@alasarr [\s\S] means capture space and non space characters.This will span multple lines as \s captures \n as well.So u need not use s flag
can you also explains ^:\n ?
@alasarr [^\n:]*: means there should not be : and ` \n` and other characters untill : if found.
0

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")]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.