Reputation: 832
I'm trying to get the contents of a string that can look like either of the two.
TITLE1: *STUFF_TO_GET* TITLE2:...
TITLE1: *STUFF_TO_GET*
my regex currently looks like this
"TITLE1:\s*?(.+?)TITLE2|$"
The reasoning is: *STUFF_TO_GET* can be flush with the colon or not which is why I include the
"\s*?"
Then the regex should grab everything until it sees TITLE2 or the end of the string. Any help is appreciated.
Upvotes: 2
Views: 116
Reputation: 149000
Alternations (|
) apply to the entire group they're in or to the entire pattern, if they are not in any groups. You haven't grouped your alternation with anything, so your version will match TITLE1:\s*?(.+?)TITLE2
or the end of the string and nothing else.
You need to group the alternation like this:
TITLE1:\s*?(.+?)(?:TITLE2|$)
It's a little strange to have those two lazy quantifiers together. If you want to allow white space before *STUFF_TO_GET*
, \s*
(no ?
) is a little bit more clear:
TITLE1:\s*(.+?)(?:TITLE2|$)
Upvotes: 2