Home > Enterprise >  How to make optional capturing groups be matched first
How to make optional capturing groups be matched first

Time:05-09

For example I want to match three values, required text, optional times and id, and the format of id is [id=100000], how can I match data correctly when text contains spaces.

my reg: (?<text>[\s\S] ) (?<times>\d )? (\[id=(?<id>\d )])?

example source text: hello world 1 [id=10000]

In this example, all of source text are matched in text

CodePudding user response:

The problem with your pattern is that matches any whitespace and non whitespace one and unlimited times, which captures everything without getting the other desired capture groups. Also, with a little help with the positive lookahead and alternate (|) , we can make the last 2 capture groups desired optional.

The final pattern (?<text>[a-zA-Z ] )(?=$|(?<times>\d )? \[id=(?<id>\d )])

  • Group text will match any letter and spaces.
  • The lookahead avoid consuming characters and we should match either the string ended, or have a number and [id=number]

Said that, regex101 with further explanation and some examples

CodePudding user response:

You could use:

:\s*(?<text>[^][:] ?)\s*(?<times>\d )? \[id=(?<id>\d )]

Explanation

  • : Match literally
  • \s* Match optional whitespace chars
  • (?<text> Group text
    • [^][:] ? match 1 occurrences of any char except [ ] :
  • ) Close group text
  • \s* Match optional whitespace chars
  • (?<times>\d )? Group times, match 1 digits
  • \[id= Match [id=
  • (?<id>\d ) Group id, match 1 digirs
  • ] Match literally

Regex demo

  • Related