title: Pattern match multiple values in a sentence
body: <p>I have a sentence that has a specific format.</p>
  <pre><code>&lt;subject&gt; &lt;action&gt; &lt;object&gt; @ &lt;price&gt; ... // The sentence can continue
  </code></pre>
  <p>and I want to extract these values out of the sentence.</p>
  <p>Constraints:</p>
  <ul>
  <li>The subject is always <code>Bob</code> or <code>Alice</code></li>
  <li>Action is either <code>bought</code> or <code>sold</code></li>
  <li>The object can be any word of 1-7 letters // <code>4apples</code> should return NULL</li>
  <li>Price is either a float/integer</li>
  <li>There can be sentences before <code>subject</code> but guaranteed to not
  contain <code>Bob/Alice</code>.</li>
  <li>There may or may not be a space after <code>@</code></li>
  </ul>
  <p>Example:</p>
  <pre><code>Hi there, Bob sold apples @2.0 dollars each
  </code></pre>
  <p>Desired Output:</p>
  <pre><code>Subject: Bob
  Action: sold
  Object: apples
  Price: 2.0
  </code></pre>
  <p>Currently, I do it the naive way by:</p>
  <pre><code>#!/usr/bin/env python3
  
  sentence = &quot;Hi there, alice sold apples @2.0 dollars each&quot;
  
  sentence = sentence.lower()
  
  if 'alice' in sentence or 'bob' in sentence:
  
      s_list = sentence.split(&quot; &quot;)
      s_idx = -1
  
      if 'bob' in sentence:
          s_idx = s_list.index('bob')
      elif 'alice' in sentence:
          s_idx = s_list.index('alice')
  
      if s_idx &gt; -1:
          Subject = s_list[s_idx]
          Action = s_list[s_idx+1]
          Object = s_list[s_idx+2]  #more if/else to validate Object contraints
          Price = s_list[s_idx+3]   #more if/else to extract 2.0 if we get @2.0 
  
      print(&quot;Subject: {}, Action: {}, Object: {}, Price: {}&quot;.format(Subject, Action, Object, Price))
  
  </code></pre>
  <p>How can I do this better? Possibly using <code>re</code></p>