0

I want to use a regex to search a string but not include part of the regex in the returned (or matched) string.

I want to search for and return what occurs after the "CLD: " but don't want the "CLD: " included in the returned (or matched) string.

cloudRegex = re.compile(r'CLD:\s.*')

From the text below I was hoping to return FEW040.

FF YBRFYMYX YMMCYMYX YSRFYMYX
061702 YBBNITAS
ATIS YBBN H   061702
RWY: 14
OPR INFO: RUNWAY 19 LEFT AVAILABLE AS PUR NOTAM.  MULTIPLE
TAXIWAY CLOSURES REFER NOTAMS. 
WND: 200/6
VIS: GREATER THAN 10 KM 
CLD: FEW040
+ TMP: 13
+ QNH: 1016
jose_bacoy
  • 12,227
  • 1
  • 20
  • 38
Tim
  • 63
  • 7
  • Thanks Wiktor. With the + matching one or more. Will that also work if the is a second word after the "CLD: "? For example, FEW040 SCT050 will be returned from CLD: FEW040 SCT050. – Tim May 06 '19 at 22:39
  • I decided to remove my comment as I found a thread that is devoted to the same problem, namely get a part of the regex match after a specific keyword. https://ideone.com/pxpeJN should return any text after that `CLD:` – Wiktor Stribiżew May 06 '19 at 22:42
  • Just an FYI, I see this all the time. There is no need to not include CLD in the match. Unless you can think of any good reason. Nothing says you have to actually _use group 0_ which is the _match object default return_. Just get group 1, or 2, or whatever you want. With Python, you don't have any other options than the look behind assertion. This time, `CLD:\s` is fixed length that Python supports. %90 of the time it will be variable length, which Python doesn't support. People reading your title might get hope, but it's no hope, it's just _DOOM_ –  May 06 '19 at 23:00

1 Answers1

0

You can use a positive lookbehind to achieve this:

(?<=CLD:\s).*

This will match the string FEW040 in your example, and can be seen working on Regex101 here.

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71