I have a short code that I am segmenting with the pipe "|" symbol and trying to match with RegEx.
The issue is everything after the 2nd segment is optional, but needs to follow a specific capture group format.
For example the 3rd segment must be a number range such as 5-10. There can be an unlimited number of segments after that that can contain any character except a pipe or a closing bracket.
The following expression works to capture everything I want:
\[tag:( )?(img|image)[ |]{1,3}[^|\]]+([ |]{1,3}[0-9]+-[0-9]+)?([ |]{1,3}[^|\]]*)*]
Unfortunately it also captures tags that are incorrectly formated.
[tag: img | some tag content, I want ] <---- Should Match
[tag: img | some tag content, I want |] <---- Should Not Match
[tag: img | some tag content, I want | 10-30 | optional-option ] <---- Should Match
[tag: img | some tag content, I want | 10-30 || optional-option ] <---- Should Not Match
How can I enforce that an optional group is captured in its entirety?
Bonus question: Why does adding a $ to the end cause nothing to match?