Need regex to match word or end of string -
i'm trying contents of string can either of two. title1: *stuff_to_get* title2:... title1: *stuff_to_get* my regex looks this "title1:\s*?(.+?)title2|$" the reasoning is: *stuff_to_get* can flush colon or not why include the "\s*?" then regex should grab until sees title2 or end of string. appreciated. alternations ( | ) apply entire group they're in or entire pattern, if not in groups. haven't grouped alternation anything, version match title1:\s*?(.+?)title2 or end of string , nothing else. you need group alternation this: title1:\s*?(.+?)(?:title2|$) it's little strange have 2 lazy quantifiers together. if want allow white space before *stuff_to_get* , \s* (no ? ) little bit more clear: title1:\s*(.+?)(?:title2|$)