Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression for Medicare MBI number format

Tags:

regex

I'm wondering if someone can help me to create a regular expression to check if a string matches the new Medicare MBI number format. Here are the specifics in regards to character position and what they can contain.

I'm using Cache ObjectScript, but any language would be helpful just so I can get the idea.

enter image description here

like image 639
Greg Avatar asked Dec 18 '22 02:12

Greg


2 Answers

If PCRE is an option, you could leverage subroutines:

(?(DEFINE)
    (?P<numeric>\d)                    # numbers
    (?P<abc>(?![SLOIBZ])[A-Z])         # A-Z without S,L,O,I,B,Z
    (?P<both>(?&numeric)|(?&abc))      # combined
)
^                                      # start of line/string
(?&numeric)(?&abc)(?&both)             # in packs of three
(?&numeric)(?&abc)(?&both)
(?&numeric)(?&abc)(?&abc)
(?&numeric)(?&numeric)
$                                      # end of line/string

Paste your IDs into the demo on regex101.com (but don't save it on regex101 or you'll expose those IDs to the public permanently).


Of course, it is not a must to use subroutines, it just makes the expression clearer, more readable and maintainable.
But, you could very well go for
^
\d
(?![SLOIBZ])[A-Z]
\d|(?![SLOIBZ])[A-Z]
\d
(?![SLOIBZ])[A-Z]
\d|(?![SLOIBZ])[A-Z]
\d
(?![SLOIBZ])[A-Z]
(?![SLOIBZ])[A-Z]
\d
\d
$

Or condensed (just copy and paste it):

^\d(?![SLOIBZ])[A-Z]\d|(?![SLOIBZ])[A-Z]\d(?![SLOIBZ])[A-Z]\d|(?![SLOIBZ])[A-Z]\d(?![SLOIBZ])[A-Z](?![SLOIBZ])[A-Z]\d\d$
like image 143
Jan Avatar answered Jan 01 '23 20:01

Jan


First position should be 1-9

https://www.cms.gov/Outreach-and-Education/Medicare-Learning-Network-MLN/MLNProducts/Downloads/MedicareCard-FactSheet-TextOnly-909365.pdf

\b[1-9][AC-HJKMNP-RT-Yac-hjkmnp-rt-y][AC-HJKMNP-RT-Yac-hjkmnp-rt-y0-9][0-9]-?[AC-HJKMNP-RT-Yac-hjkmnp-rt-y][AC-HJKMNP-RT-Yac-hjkmnp-rt-y0-9][0-9]-?[AC-HJKMNP-RT-Yac-hjkmnp-rt-y]{2}\d{2}\b
like image 45
Sam Huang Avatar answered Jan 01 '23 20:01

Sam Huang