javaregexstring-matching

How to find optional group with some prefix using Regex


This is my pattern regex:

"subcategory.html?.*id=(.*?)&.*title=(.+)?"

for below input

http://example.com/xyz/subcategory.html?id=3000080292&backTitle=Back&title=BabySale I want to capturebelow group

For which it is working fine. The problem is I want to make second group i.e. value of title to be optional, so that even if title is not present, regex should match and get me value of group 1(id). But for input

http://example.com/xyz/subcategory.html?id=3000080292&backTitle=Back&

Regex match is failing even if group one is present. So my question is how to make second group optional here?


Solution

  • Maybe make the entire substring optional?

    Try subcategory.html?.*id=(.*?)&.*(?:title=(.+)?)?

    Also note that your (and my) regex might be matching too much. For example, the dot here should probably be escaped: subcategory\.html instead of subcategory.html or you will match subcategory€html, too. Your question mark says the l of html is optional; you are probably saved by the .* ("match anything"), that follows.

    Last but not least, the final .* means that even this will match (which you probably don't want to match):

    http://example.com/xyz/subcategory.html?id=3000080292&backTitle=Back&title=BabySale&Lorem Ipsum Sit Atem http://&%$
    

    It's usually a bad idea to match .* as it will nearly always match too much. Consider using character classes instead of the dot, and to anchor he beginning (^) and end ($) of the string... :)