title: How to create dictionary from a string using a regex to get groups?
body: <p>I have complex task that I want to accomplish:
  From a string I want to be able to classify words in particular categories.</p>
  <pre><code>s = 'Age 63 years, female 35%; race or ethnic group: White 68%, Black 5%, Asian 19%, other 8%'
  d = function(s)
  print(d)
        {&quot;age&quot;: &quot;63 years&quot;,
         &quot;gender: &quot;female 35%&quot;,
         &quot;race&quot;: &quot;White 68%, Black 5%, Asian 19%, other 8%&quot;}
  </code></pre>
  <p>I must not that not all strings are in the same format but there is a finite set of categories in all (<code>age</code>, <code>gender</code>, <code>race</code>, <code>region</code>) but some strings only have 1 or 2 out of the 4 categories.</p>
  <p>Here are some other toy strings:</p>
  <pre><code>s2 = 'Age 71 years, male 64%'
  s3 = 'Age 64 years, female 21%,
  Race or ethnicity: White 66%, Black 5%, Asian 18%, other 11%
  Region: N. America 7%, Latin America 17%, W. Europe or other 24%, central Europe 33%, Asia-Pacific 18%
  </code></pre>
  <p>As you can see there are some patterns:</p>
  <ul>
  <li><code>age</code> is not preceded by any <code>':'</code>.</li>
  <li><code>gender</code> is documented as either female or male.</li>
  <li><code>race</code> and <code>region</code> are followed by <code>':'</code>.</li>
  </ul>
  <p>I am in interested in collection all the information corresponding to the category as see in my toy example with the race category.</p>
  <p>What I need:</p>
  <ol>
  <li>Writing the RegEx pattern with the appropriate capturing groups to obtain the results.</li>
  <li>Transform the matches to a dictionary: I have seen a solution using the <code>.groupdict()</code> method to do so.</li>
  </ol>
  <p>I have a problem writing the regex pattern that will return the aforementioned groups.</p>
  <p>I have seen this interesting solution for a similar problem: <a href="https://stackoverflow.com/questions/56240304/python-regex-create-dictionary-from-string">python regex: create dictionary from string</a>.
  But I have trouble applying it to mine.</p>