Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex to match a string or an abbreviation thereof

For writing a parser I need to be able to identify keywords which can be abbreviated,
for example

MY-KEYWORD

should at least be MY-KEY but can also be any abbreviation longer than this, here specifically MY-KEYW, MY-KEYWO, MY-KEYWOR or the full MY-KEYWORD.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For the life of me, no regex I tried so far (and that were many …) matches exact substrings of something with a minimum length 🙁

TIA !

Alex

>Solution :

Match the prefix and then optional characters after it to finish the full keyword.

\bMY-KEY(?:W(?:O(?:RD?)?)?\b

All the groups are needed to ensure that no optional letters are skipped. If you wrote MY-KEYW?O?R?D it would match MY-KEYD.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading