Home > Software design >  Is there a way to set the precedence of capturing groups
Is there a way to set the precedence of capturing groups

Time:05-20

I want 3 capturing groups (pre/mid/post) that share some characters. All three can match 0 or 1 time, something like this:

^(?<pre>[T])?(?<mid>[T][A-Z])?(?<post>[T])?$

I am looking for them to match in order: mid -> pre -> post. Regex I've posted works for 3/4 letter strings, like TTT / TTU / TUT. For two character string TT, I would like for group mid to be matched - the result is pre: T, post: T though. I understand why that's happening - pre matches, mid doesn't find two character to match, post matches.

For mid to take precedence I've tried combinations of groups and lazy operators, but most of them resulted in post matching before pre, like for this example:

^(?<pre>[T])??(?<mid>[T][A-Z])?(?<post>[T])?$

Is there any way to allow mid group to match before pre? Can you set some kind of order of matching?

CodePudding user response:

I don't think you need any lazy operators - just explicitly group pre together with mid, so that if pre matches then mid is required:

^(?:(?<pre>T)?(?<mid>T[A-Z]))?(?<post>T)?$

(online demo)

Or rather, I think you want to require mid to match together with an optional post, so that a single T input is matched by pre only:

^(?<pre>T)?(?:(?<mid>T[A-Z])(?<post>T)?)?$

(online demo)

  • Related