Regular expression to match wordpress like shortcodes, for both self closing and enclosed <p>I am trying to get a regular expression pattern to implement a shortcode feature in Joomla CMS, using plugins, similar to WordPress.</p> <p>The shortcode may be a self closing one like <code>{myshortcode shortcode="codeone"}</code> at some occassions and may be an enclosed ones like:</p> <pre><code>{myshortcode shortcode="anothercode"|param="test"} {column}column 1{/column} {/myshortcode} </code></pre> <p>I managed to figure out the regular expression used in WordPress:</p> <pre><code>{(}?)(myshortcode)(?![\w-])([^}\/]*(?:\/(?!})[^}\/]*)*?)(?:(\/)}|}(?:([^{]*+(?:{(?!\/\2})[^{]*+)*+){\/\2})?)(}?) </code></pre> <p>This is working fine if the content contains one or more self closing shortcodes, or one or more of the enclosed shortcodes. But if the content contains a mixture of both, it won't work. </p> <p>Instead of using the square brackets <code>[]</code>, I am using the curly braces <code>{}</code>, as this is how content plugins are used in Joomla.</p> <p>Now please check the below HTML snippet:</p> <pre><code>&lt;div id="lipsum"&gt; &lt;p&gt; Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin pellentesque lectus tellus, ut tincidunt orci posuere non. Aliquam erat volutpat. Phasellus in lobortis dolor, porta varius nunc. Ut et felis rutrum, pharetra mi a, ullamcorper purus. In vitae fringilla velit. In nec scelerisque mauris, sed eleifend urna. Duis feugiat risus et arcu eleifend venenatis. &lt;/p&gt; &lt;h3&gt;Shortcode 1&lt;/h3&gt; &lt;p&gt;{myshortcode shortcode="codeone"}&lt;/p&gt; &lt;h3&gt;Shortcode 2 - &lt;/h3&gt; &lt;p&gt;{myshortcode shortcode="anotherone"|param="test"} {column}column 1{/column} {/myshortcode} &lt;/p&gt; &lt;p&gt; Duis quis nisl fringilla, porttitor tellus a, congue mauris. Sed posuere erat vel metus egestas, eget lobortis dolor pretium. Proin iaculis pharetra consectetur. Sed in enim ultricies, sagittis nisl vitae, porttitor libero. Praesent ut erat nisi. Maecenas luctus magna lacus. Mauris ullamcorper maximus arcu et tincidunt. Aenean cursus enim blandit, scelerisque ex sed, vestibulum felis. In magna massa, sagittis in eleifend vel, tristique vitae nisi. &lt;/p&gt; &lt;/div&gt; </code></pre> <p>Here the HTML contains both self enclosing and enclosing shortcodes. In this case the above regular expression pattern is not working, as it matches the two shortcodes as one, starting from <code>{myshortcode shortcode="codeone"}</code> to <code>{/myshortcode}</code> as a single shortcode.</p> <p><strong>So my question is is there a pattern I can use to match both the shortcodes?</strong></p> <p>Please check the current state of it here <a href="http://regex101.com/r/tF0mA9/1" rel="nofollow">http://regex101.com/r/tF0mA9/1</a>. Here I am expecting two matches.</p> <p>Thank you for your help.</p>