title: How to make some parts of a regex pattern conditional?
body: <p>I checked this forum for a answer to my problem, but couldnt find. Experts please help.</p>
  
  <p>I have a problem to validate a string say first name. I am given a set of rules which the string needs to honor and the same needs to be confirmed with regex. I wrote a pattern. Except one rule my pattern honors everything else. I am listing my regex pattern below</p>
  
  <pre><code>([A-Za-z]+[-'!` ]?)*
  </code></pre>
  
  <p>I am checking for a string say first or last name which needs to start with a alphabet and can have any one of (- or ! or ' or ` or spaces ) these 5 characters ONE AND ONLY IF there is a second word. If a name has 2 words with a space in between then its ok. some examples</p>
  
  <pre><code>List of Valid Names
  TIMOTHY
  JONATHON
  PATRICK
  B`ELLA
  SUZY JANE - Only one space allowed
  SUZY-JANE
  
  List of Invalid Ones
  T~ELLA - Because this ~ is not part of regex
  SUZY - JANE - No space allowed between hyphens
  SUZY  JANE - 2 spaces betweeb words not allowed
  GRACO&amp;LAME - &amp; is not allowed
  </code></pre>
  
  <p>My regex passes all these above conditions But fails when the name ends in any one of the special character. For example, if I give JOHN- OR JOHN' OR JOHN! OR JOHN` OR JOHN (with s spaces at the end) these are not valid but my regex isnt able to handle it</p>
  
  <p>Can some one tell me how to write a conditional regex? Like one and only if there is a second word then the regex pattern must allow the entry of these special characters.</p>