-1

I'm trying to make a group at the end optional. But it includes one excess result. What the right regex could look like? Would appreciate any help here.

Regex = <!([a-z]{0,25})\|([^\|>]*)\|([^\|<>]*)(\|([^\|<>]*))?>

Example = <!user|123|Kirill|{"color":"rgb(255, 184, 75)"}> published <!content|456|A cool content>

Gives the following 1st matched group, the highlighted is the excess unexpected result: enter image description here

Valerii
  • 395
  • 1
  • 2
  • 8

1 Answers1

1

The pipe could be outside of the last capture group, then make that whole part optional using a non capture group.

Note that you don't have to escape the pipe in a character class.

<!([a-z]{0,25})\|([^|>]*)\|([^|<>]*)(?:\|([^|<>]*))?>

Regex demo

enter image description here

The fourth bird
  • 154,723
  • 16
  • 55
  • 70