title: Obtain word before/after a character with regex in Python
body: <p>This is supposed to be easy using capturing groups, but I am not getting the correct words. I have been using the following:</p>
  
  <pre><code>#Before
  print(re.sub(r'\b([A-Za-z0-9]+)\b(?=\.?\s*(\&amp;|\-|and))',r'\1','A. &amp; B.',flags=re.IGNORECASE))
  A. &amp; B.
  
  #After
  print(re.sub(r'(\&amp;|\-|and)\s*\b([A-Za-z0-9]+)\b',r'\2','A. &amp; B.',flags=re.IGNORECASE))
  A. B.
  </code></pre>
  
  <p>The string can be one of the following:</p>
  
  <pre><code>A. - B.
  A.-B.
  A. &amp; B.
  A.&amp;B.
  A. AND B.
  </code></pre>
  
  <p>The idea is to get the word before/after of <code>ampersand| hyphen | and</code> I divided in two regex to get both words. In this example, before would get just <code>A</code> and the after <code>B</code>.</p>
  
  <p>Why the capturing groups are not printing <code>A</code> and <code>B</code> in the previous examples?</p>
  
  <p>Thanks in advance :)</p>