0

I inherited code from someone else, and I can't understand the middle of the regular expression. I've googled regex cheat sheets online, but none of them touch on what S[KD]|HH|H might mean.

Could someone help me backwards interpret this?

^([0-9][[:upper:]])  ([0-9]{1,3}|S[KD]|HH|H) ?([A-P,R-S]|Q[0-9]|Q)?$

I understand the first section is looking for a single digit and an uppercase letter. I can try and puzzle my way through the last section. But that middle section is a doozy for me.

I would be looking for a translation like "Any number of 1 to 3 digits long, or the character S and then KD together, or two H's, or a single H." (I don't think that's the right interpretation, but that has been my best guess).

Thank you so much in advance. I know these regex questions are common and not fun.

ACK
  • 39
  • 1
  • 1
  • 8
  • 1
    Have you tried pasting the regex code in online regex compliers like https://regexr.com/ ? They would give you the accurate explanation imo. – Ronak Shah Sep 24 '21 at 04:05
  • Another option is https://regex101.com/. If you put in your regex it gives an "explanation" in words of what it's doing in the top right. – MrFlick Sep 24 '21 at 04:06
  • The `|` is the way you do "or" https://www.ocpsoft.org/tutorials/regular-expressions/or-in-regex/ and the `[]` is a character class: https://www.regular-expressions.info/charclass.html – MrFlick Sep 24 '21 at 04:09
  • “[KD]” means either K or D but not both together. As MrFlick says it’s a character class expression. – IRTFM Sep 24 '21 at 04:48
  • The explanation is on the right [here](https://regex101.com/r/Bnzyen/1). – Wiktor Stribiżew Sep 24 '21 at 07:12

0 Answers0