Input:
:CITYNAMEONE SCHEDULED
:CITY NAME TWO
:CITYNAMETHREE
Tried:
:([A-Z\s]+)(?=SCHEDULED)
:([A-Z\s]+)(?:SCHEDULED)?
Expecting:
CITYNAMEONE
CITY NAME TWO
CITYNAMETHREE
Input:
:CITYNAMEONE SCHEDULED
:CITY NAME TWO
:CITYNAMETHREE
Tried:
:([A-Z\s]+)(?=SCHEDULED)
:([A-Z\s]+)(?:SCHEDULED)?
Expecting:
CITYNAMEONE
CITY NAME TWO
CITYNAMETHREE
You can use
:(.*?)(?=\s*SCHEDULED|$)
See the regex demo.
Details:
:
- a colon(.*?)
- Group 1: zero or more chars other than line break chars as few as possible(?=\s*SCHEDULED|$)
- a positive lookahead that requires zero or more whitespaces or end of string immediately to the right of the current location.To keep the capital letter genre and to consume only words that are not SCHEDULE this is ideal
:(?:\s*(?!SCHEDULED)[A-Z]+)+
https://regex101.com/r/3GefdG/1
:
(?:
\s*
(?! SCHEDULED )
[A-Z]+
)+
If need to trim white space at the beginning as well, , a capture group is needed.
:\s*((?!SCHEDULED)[A-Z]+(?:\s*(?!SCHEDULED)[A-Z]+)*)
https://regex101.com/r/dmcXpQ/1
:
\s*
( # (1 start)
(?! SCHEDULED )
[A-Z]+
(?:
\s*
(?! SCHEDULED )
[A-Z]+
)*
) # (1 end)