title: Regex multiple match substring
body: <p>I've got an application which determines, given a perl regex, if it should display a dropdown menu or a simple input field. Therefore, I have to check the regex pattern for an "outer form" and substrings. For this, I came up with several solutions.</p>
  
  <p>Given the input pattern "^(100|500|1000)$", which should result in a drop down menu with three entries, 100, 500 and 1000. I need one regex which parses the entire pattern, to determine if it is a valid list, and one regex that does the actual substring match, since I don't know how to match one substring multiple times. This is my regex pattern:</p>
  
  <pre><code>^\^\((?:((?:[^\|]|\\\|)+)(?:\||(?:\)\$$)))+
  </code></pre>
  
  <p>A little bit of simplification, since this regex is a little bit fuzzy:</p>
  
  <pre><code>^\^\((?:([\w\d]+)(?:\||(?:\)\$$)))+
  </code></pre>
  
  <p>This works, but only stores the last substring (1000 in the given case) and throws the rest away, tested with either PCRE and online regex tools. To get the actual substrings, i.e. dropdown menu fields, I have:</p>
  
  <pre><code>(?:\^\()?((?:[^\|]|\\|)+)(?:\||(?:\)\$$))
  </code></pre>
  
  <p>Simplification again:</p>
  
  <pre><code>(?:\^\()?([\w\d]+)(?:\||(?:\)\$$))
  </code></pre>
  
  <p>This matches the substring but doesn't match the dropdown menu pattern syntax which the other regex does (this one also matches "^(100|" with substring "100", for example).
  My question is: is there a way to combine these regular expressions to have just one pattern that matches 1) the entire pattern syntax and 2) the actual substrings?</p>
  
  <p>Thanks in advance,</p>
  
  <p>Jeremy</p>
  
  <p>P.S.: sorry if this is obvious, but I'm very bit tangled about all these regular expressions today.</p>
  
  <h2>Sample data:</h2>
  
  <p>Input regex: ^(100|500|1000)$<br>
  Syntax OK!<br>
  Matched substrings: 100, 500, 1000<br>
  => show dropdown menu  </p>
  
  <p>Input regex: ^[0-9a-fA-F]+$<br>
  Syntax is wrong!<br>
  => show regular input field  </p>
  
  <p>Input regex: ^(foo|bar)$<br>
  Syntax OK!<br>
  Matched substrings: "foo", "bar"<br>
  => show dropdown menu</p>
  
  <p>Input regex: ^(foo|bar)[0-9]+$<br>
  Syntax is wrong!<br>
  => show regular input field  </p>