title: ERROR: invalid regular expression: parentheses () not balanced while performing the left join in Postgres 11.0
body: <p>I have following table in postgres 11.0</p>
  <pre><code>atc_code_no code    level   text
  1           A       1       ALIMENTARY TRACT AND METABOLISM
  2           A01     2       STOMATOLOGICAL PREPARATIONS
  3           A01A    3       STOMATOLOGICAL PREPARATIONS
  3           A01W    3       (herbal stomatological remedies
  4           A01AA   4       Caries prophylactic agents
  5           A01AB   4       Antiinfectives and antiseptics for local oral treatment
  7           A01AD   4       Other agents for local oral treatment
  </code></pre>
  <p>I have written following query with 'with' statements and then joining the tables using left join and substring match.</p>
  <pre><code>with level1 as(
          select CONCAT(&quot;code&quot;, ' (', lower(&quot;text&quot;), ')') as atc_code_level1 from s0_atc_code
          where level = '1'
          ),
          
          level2 as(
          select CONCAT(&quot;code&quot;, ' (', lower(&quot;text&quot;), ')') as atc_code_level2 from s0_atc_code
          where level = '2'
          ) ,
          
          level3 as(
          select CONCAT(&quot;code&quot;, ' (', lower(&quot;text&quot;), ')') as atc_code_level3 from s0_atc_code
          where level = '3'
          ) ,
          
          level4 as(
          select CONCAT(&quot;code&quot;, ' (', lower(&quot;text&quot;), ')') as atc_code_level4 from s0_atc_code
          where level = '4'
          )
  
         SELECT level1.*,
                level2.*,
                level3.*,
                level4.*
                
         FROM level1
         LEFT JOIN level2 ON btrim(SUBSTRING(level2.atc_code_level2, '(.*) \(')) ~ btrim(SUBSTRING(level1.atc_code_level1, '(.*) \('))
         LEFT JOIN level3 ON btrim(SUBSTRING(level3.atc_code_level3, '(.*) \(')) ~ btrim(SUBSTRING(level2.atc_code_level2, '(.*) \('))
         LEFT JOIN level4 ON btrim(SUBSTRING(level4.atc_code_level4, '(.*) \(')) ~ btrim(SUBSTRING(level3.atc_code_level3, '(.*) \('))
  </code></pre>
  <p>As long as, I am joining level1, level2, and level3, the query can join and provide me the output but when I join level4 and level3, I got an error:</p>
  <p>[Code: 0, SQL State: 2201B]  ERROR: invalid regular expression: parentheses () not balanced</p>
  <p>Following should be the output after the join.</p>
  <p>I am having following table in Postgres 11.0</p>
  <pre><code>atc_code_level1                     atc_code_level2                     atc_code_level3                        atc_code_level4
  A (alimentary tract and metabolism) A01 (stomatological preparations)   A01A (stomatological preparations)  A01AD (other agents for local oral treatment)
  A (alimentary tract and metabolism) A01 (stomatological preparations)   A01A (herbal stomatological remedies)   A01AA (caries prophylactic agents)
  A (alimentary tract and metabolism) A01 (stomatological preparations)   A01A (herbal stomatological remedies)   A01AB (antiinfectives and antiseptics for local oral treatment)
  </code></pre>
  <p>Any help is highly appreciated.</p>