title: How to parse out two fields out of string using regex in Python?
body: <p>I'm trying to figure out how to use regex to parse out fields from a naming scheme. Basically, a way to go through a query string and pull out patterns based on the naming scheme.  In this case, there are two patterns to pull out which are the ID and the DIRECTION.</p>
  <p><code>DIRECTION</code> will always be either 1 or 2</p>
  <p><code>ID</code> can be any string that is allowed for file systems (e.g., alphanumeric - _ .)</p>
  <p>Here is the basic framework I'm trying to code up:</p>
  <pre class="lang-py prettyprint-override"><code>def function(query:str, naming_scheme:str):
      # stuff
      return (ID, DIRECTION)
  </code></pre>
  <p>Here is a query for naming scheme 1 (<code>naming_scheme_1</code>):</p>
  <pre class="lang-py prettyprint-override"><code>naming_scheme_1 = &quot;[ID]_R[DIRECTION].fastq.gz&quot;
  ID, DIRECTION = function(&quot;Kuwait_110_S59_R1.fastq.gz&quot;, naming_scheme_1)
  #ID = &quot;Kuwait_110_S59&quot;
  #DIRECTION = &quot;1&quot;
  
  ID, DIRECTION = function(&quot;Kuwait_110_S59_R2.fastq.gz&quot;, naming_scheme_1)
  #ID = &quot;Kuwait_110_S59&quot;
  #DIRECTION = &quot;2&quot;
  </code></pre>
  <p>Here is a query for naming scheme 2 (<code>naming_scheme_2</code>):</p>
  <pre class="lang-py prettyprint-override"><code>naming_scheme_2 = &quot;[ID]_R[DIRECTION]_001.fastq.gz&quot;
  ID, DIRECTION = function(&quot;Kuwait_110_S59_R1_001.fastq.gz&quot;, naming_scheme_2)
  #ID = &quot;Kuwait_110_S59&quot;
  #DIRECTION = &quot;1&quot;
  
  ID, DIRECTION = function(&quot;Kuwait_110_S59_R2_001.fastq.gz&quot;, naming_scheme_2)
  #ID = &quot;Kuwait_110_S59&quot;
  #DIRECTION = &quot;2&quot;
  </code></pre>
  <p>Here is a query for naming scheme 3 (<code>naming_scheme_3</code>):</p>
  <pre class="lang-py prettyprint-override"><code>naming_scheme_3 = &quot;barcode-[ID]_[DIRECTION].fq&quot;
  
  ID, DIRECTION = function(&quot;barcode-Kuwait_110_S59_1.fq&quot;, naming_scheme_3)
  ID = &quot;Kuwait_110_S59&quot;
  DIRECTION = &quot;1&quot;
  
  ID, DIRECTION = function(&quot;barcode-Kuwait_110_S59_2.fq&quot;, naming_scheme_3)
  ID = &quot;Kuwait_110_S59&quot;
  DIRECTION = &quot;2&quot;
  </code></pre>
  <p><strong>How can I use regex (or similar) in Python to parse out fields in this context?</strong></p>
  <p>My current method is to do a series of splitting events on a string which doesn't seem like the best option.</p>