Looking for a regular expression used on variable string
<p>I have a string that can vary slightly:</p>

<pre><code>str1 = "[X] text [Y] abc123"
str1 = "(X ) text [y ]  abc123"
str1 = "(x) text (Y)        abc123"
str1 = "(X ) text   (Y)  abc123"
str1 = "(X ) text [Y ]  abc123"
str1 = "[X]   text333 [y]  abc123222"
</code></pre>

<p>so basically X and Y are static and they can either be between <code>()</code> or <code>[]</code>. My problem is, that there can be some white spaces as well. X and Y with bracket can be assumed as delimiter to capture group 1. I am currently asking myself how to parse "text" and "abc123" in the most reliable way. How would you do it? "text" and "abc123" are variable strings. I want to use them further</p>

<p>How about:</p>

<pre><code>[\[)]X[\])]\s*(.+?)\s*[\[(]Y[\])]\s*(.+)
</code></pre>

<p><code>text</code> will be in group 1 and <code>abc123</code> in group 2</p>

<p><strong>Explanation:</strong></p>

<pre><code>[\[)]   : character class `[` or `(`
X       : whatever X stands for
[\])]   : character class `]` or `)`
\s*     : 0 or more space character
(.+?)   : 1 or more any character non greedy, captured in group 1
\s*     : 0 or more space character
[\[(]   : character class `[` or `(`
Y       : whatever X stands for
[\])]   : character class `]` or `)`
\s*     : 0 or more space character
(.+)    : 1 or more any character, captured in group 2
</code></pre>

python regex