Home > Net >  Optional groups in a regular expression
Optional groups in a regular expression

Time:01-13

For some reason I fail to write a correct regular expression to match the following strings:

a/b/c/d:d0/d1/d2/d3/:e;f'

a/b/c/d:d0/:e;f'

a/b/c/d:d0/:e'

a/b/c/d:d0'

In each string the c and e should be extracted. As you see, e is optional and the last string doesn't contain it. In that case the regular expression should still match and return the c.

This is the expression that I came up with, but it does not support an optional e:

a\/b\/(?<the_c>\w*)\/.*?\/:(?<the_e>\w*)

I thought to make the last part optional, but then it just doesn't find the e at all:

a\/b\/(?<the_c>\w*)\/.*?(?:\/:(?<the_e>\w*))?
                        ^^^                ^^

Here is a link to test out this example: https://regex101.com/r/C2Jkhq/1

What's wrong with my regex here?

CodePudding user response:

You can use

a\/b\/(?<the_c>\w*)\/(?:.*\/:(?<the_e>\w*))?

Details:

  • a\/b\/ - a/b/ string
  • (?<the_c>\w*) - zero or more word chars captured into "the_c" group
  • \/ - a / char
  • (?:.*\/:(?<the_e>\w*))? - an optional sequence (that is tried at least once) matching:
    • .* - any zero or more chars other than line break chars as many as possible
    • \/: - /: string
    • (?<the_e>\w*) - zero or more word chars captured into "the_e" group .

See this regex demo.

  • Related