Reputation:
I am trying to find a regular expression that matches some custom tags from a text. The tags always have the format [!--sometext--] or [!--sometext:param1--], [!--sometext:param1:param2--]. What I need to do is fetch the "sometext" part and the optional params as "param1:param2" or "param1" and "param2" separately (doesn't matter).
My approach for tags without params is
\[!--(.+?)--\]
but how do I match the params part in an elegant way?
Upvotes: 1
Views: 225
Reputation: 12571
Update: OK, third time should be the charm, right? ;-) This works for me in a sample Perl script.
This should do it (and I'm assuming a Perl-compatible reg-ex ... you don't specify what language you're writing this in):
\[!--([^\]:]+):?([^\]-]+?)?--\]
What's going on here?
[!--
:
or a ]
-
or a ]
(this should capture all the optional parameters at once)--]
You'll need to split the optional parameters on :
to catch the case where there might be more than one.
Upvotes: 1