I’m pretty new to regular expression. Just tried to analyse a “BB-Code” that could do something like this:
Pattern:
\[element title=(.*)picture=(\d+)](.*)(?:\(.*):?\)?\[/caption].*
Search:
[element title=element title picture=32]Lorem ipsum dolorPhoto by John Doe[/element]
[element title=element title picture=32]Lorem ipsum dolor[/element]
Well, the caption-part should be optional and both entries should give results. How can I reach this?
How about this:
It will match both:
Example
in PHP, you can use it this way:
The array
$matchwill have several elements. Those are the strings that are surrounded by round-brackets(and)in the regular expression. One of them is the caption text.Program execution and output can be seen here
http://ideone.com/vQ1T0.