Reputation: 108
I use the PECL BBCode class in an older project to manage BBCodes, which works very well. For some special cases, it doesn't suffice, because I need to run a function over input. So I want to do this with regex before the PECL thing is runnning.
I want to match the [member]-tag with as well as without attribute:
[member]Donald Duck[/member]
[member=Dr. Donald Duck]Donald Duck[/member]
I can match them this way:
\[member\](.+?)\[\/member\]
\[member=(.+?)\](.+?)\[\/member\]
How can I do this in one step? I made the = optional with ? but it doesn't match both. Thank you for your support.
Upvotes: 1
Views: 86
Reputation: 626929
You should add =
to the optional group:
\[member(=.*?)?](.+?)\[\/member]
^^^^^^^
See the regex demo
Details
\[member
- a literal [member
substring(=.*?)?
- Group 1: an optional sequence of =
and then any 0+ chars other than line break chars, as few as possible]
- a ]
char (no need to escape it)(.+?)
- Group 2: one or more chars other than line break chars, as few as possible\[\/member]
- a literal [/member]
substring.Upvotes: 1