title: Regex code is too long and inefficient. I want to make it better
body: <p>Edit: Okay, my apologies because my first phrasing of the question over simplistic that the answers which are technically correct didn't work with my case, so I'll try to be as detailed as possible this time.</p>
  <p>I am working on constructing a regex to capture a single or compound codes.
  Each code would start with &quot;H&quot;, &quot;G&quot;, &quot;EUH&quot;, or &quot;AUG&quot;, followed by either three digits(H123) or three digits, a dot, and one digit. (H123.1).
  First digit must not be a zero.</p>
  <p>Each H code could be followed by one or more of these characters: &quot;idDfF&quot;.(H231Df).
  Each P code could be followed by one or more of these characters: &quot;AEBDab&quot;.(P333a).
  Each EUH code could be followed by &quot;A&quot;.(EUH123A).</p>
  <p>Example for compounds: H123Df+H234+H234.1+H342.1Fd, and P123.1+P555D
  Spaces could be anywhere in the string except the 3 digits, those always follow each other with no spaces.
  Dots or dashes could be before the extra characters at the end (H123-Df, H123.Df)</p>
  <p>I need to match every compound or single code. Please note that in my previous try, I'd detect H567+H890+H654 correct but also H567 and H890 and H654, this is wrong. Once a set is matched, its inner codes shouldn't be treated as single codes anymore.</p>
  <p>My current approach right now is actually 4 different regex codes, one for each set of codes. It mostly works (I guess) but it is very tedious and wouldn't work if another code is added to a compound and over all very unmaintainable with more complex stuff added to the regex.
  Here's an example for the regex for the P codes.</p>
  <pre><code>`p = &quot;P\s*\d{3}[.\s-]*[AEBDab]*\s*[+/]\s*P*\s*\d{3}[.\s-]*[AEBDab]*\s*[+/]\s*P*\s*\d{3}[.\s-]*[AEBDab]*|P\s*\d{3}[.\s-]*[AEBDab]*\s*[+/]\s*P*\s*\d{3}[.\s-][AEBDab]*|P\s*\d{3}[.\s-]*[AEBDab]*\s`&quot;
  </code></pre>
  <p>As you can see, it's very long and inefficient, and to edit a minor thing would take so much time and be very prone to errors.</p>
  <p>How can I achieve this in a better way? Is making 4 regex's better or just one for catching all formats of all 4 codes?</p>
  <p>Edit:
  A realistic version would look exactly like this:</p>
  <pre><code>text = &quot;random text
  H326random text H243Df+H546+H677-Drandom text
  randomtext P234a+P333.Ddrandom text
  someother randomtexts&quot;
  
  output = [H326, H243+H546+H677, H234+H333]
  </code></pre>
  <p>note: spaces are very random in the texts provided because they are PDF texts parsed with pdfminer library, it messes up the order and spaces very much, but it was the only library I found that could actually &quot;read&quot; all the 10k PDF files I have, other libraries would yield empty strings and or just read parts of the PDF.</p>
  <p>Note: if you could explain your answer, because as I am working I'm always discovering new formats and I want to be able to edit the regex as I want.</p>
  <p>Thank you!</p>