Search code examples
regexregex-group

need help on regex when using a quantifier after a group


I would like to match either of the strings - abc or abc* or abc? or abc{abc}.The following .*?(\?|\*|\{.*?\}) regex matches all the above except abc. Even .*?(\?|\*|\{.*?\})? does not work.

Any help will be appreciated. (I am using java)


Solution

  • Check if the below pattern help?

    Demo: https://regex101.com/r/XtEPSe/2

    Pattern: .+?(\?|\*|\{.*?\}|$)|(?:^$)

    Detail:

    • Changed .*? to .+?. To match empty string added a alternate ^$ at the end. Plese remove it if it is not required.
    • Added an alternate $ to match abc
    • The capture group 1 should give you the required characters.