Regex to match everything after a Question Mark

We have already discussed in this blog about how to match everything after a specific word, but that regex is not suitable here because it contains question mark which is a quantifier in regex. With little adjustment with the code, we can match everything after a question mark.

By default, quantifiers in the regex like (*,?,+) cannot be used as string until backward slash is with them. Example \?

Backward slash with the quantifier, allows them to behave like string and we can simply use them as string in any regex.


(?<=\?)(.*)

This regex will select everything after question mark (not including question mark itself)

Check on Regex101

(?=\?)(.*)

This will select from the question mark.

Check on Regex101


Explanation of the Regex

Positive Lookbehind (?<=\?) Assert that the Regex below matches\? matches the character ? with index 6310 (3F16 or 778) literally (case sensitive)

1st Capturing Group (.*). matches any character (except for line terminators)* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)

Leave a Comment