Regex for nested pattern : <p>I have a pattern like this:</p>

<pre><code>#alphanumericX(anythingY){
    anythingZ
}#
</code></pre>

<p>It can be nested like this:</p>

<pre><code>#alphanumeric1(anything1){
    #alphanumericX(anythingY){
        anythingZ
    }#
}#
</code></pre>

<p>Or like this:</p>

<pre><code>#alphanumeric1(anything1){
    anything2
    #alphanumericX(anythingY){
        anythingZ
    }#
}#
</code></pre>

<p>The patterns can be written not just once, but multiple like this:</p>

<pre><code>#alphanumericX(anythingY){
    anythingZ
}#
#alphanumeric1(anything1){
    anything1
}#
</code></pre>

<p>I want a regex to match the the patterns above, so it will return like this:</p>

<pre><code>#alphanumericX(anythingY){
    anythingZ
}#
</code></pre>

<p>How to achieve that?</p>

<p>Here is my current regex: <code>/#(.*?)\((.*?)\)\{(.*?)\}#/sm</code></p>
