1

So I am trying to create a regex for MBIs (medical beneficiary identifiers) in python and my regexes wont work.

Any example of a MBi is: 1EG4-TE5-MK73

Here is the format to MBIs:

  • 11-Characters

  • The MBI’s 2nd, 5th, 8th, and 9th characters will always be a letter.

  • Characters 1, 4, 7, 10, and 11 will always be a number.

I have tried using the following regex but to no avail:

mbi=re.compile(r"[1-9]{1}[^SLOIBZsloibz|^0-9]{1}[^SLOIBZsloibz]{1}[0-9]{1}-?[^SLOIBZsloibz|^0-9]{1}[^SLOIBZsloibz]{1}[0-9]{1}-?[^SLOIBZsloibz|^0-9]{1}[^SLOIBZsloibz|^0-9]{1}[0-9]{1}[0-9]{1}")

Any suggestions?!?!

James Davinport
  • 293
  • 3
  • 14

3 Answers3

2

Here is the format to MBIs:

  1. 11-Characters
  2. The MBI’s 2nd, 5th, 8th, and 9th characters will always be a letter.
  3. Characters 1, 4, 7, 10, and 11 will always be a number.

There's no mention about the 3rd and 6th characters, but based on your example, you can use a regex like:

\b\d[A-Z]{2}\d-[A-Z]{2}\d-[A-Z]{2}\d{2}\b

Regex Demo


Regex Explanation: enter image description here

Pedro Lobito
  • 75,541
  • 25
  • 200
  • 222
1

Ok, so based upon your regex, there were certain things that you tried doing were syntactically not correct. Quantifying through curly braces is not needed when you want it to occur just only once due to which {1} becomes redundant. So I've removed this part from your regex. Second thing in your character set,

[^SLOIBZsloibz|^0-9]

as I can see you want to negate certain alphabets and 0-9 digits. You don't have to apply alternation while you are using character set []. Besides negating those alphabets in your character set, if you also want to negate 0-9 number, you just have to simply put it there and your intended correct character set becomes,

[^SLOIBZsloibz0-9]

After applying these corrections in your regex, your regex becomes this, which is what you needed I guess.

^[1-9][^SLOIBZsloibz0-9][^SLOIBZsloibz][0-9]-?[^SLOIBZsloibz0-9][^SLOIBZsloibz][0-9]-?[^SLOIBZsloibz0-9][^SLOIBZsloibz0-9][0-9][0-9]$

Demo

Let me know if this works fine for you.

Pushpesh Kumar Rajwanshi
  • 17,850
  • 2
  • 16
  • 35
0

I added in a few \\- so it wouldn't count the hash as a valid alpha-numeric character.

^[1-9][^SLOIBZsloibz0-9][^SLOIBZsloibz][0-9]-?[^SLOIBZsloibz0-9\\-][^SLOIBZsloibz\\-][0-9]-?[^SLOIBZsloibz0-9\\-][^SLOIBZsloibz0-9\\-][0-9][0-9]$
Ruli
  • 2,184
  • 10
  • 21
  • 30