0

I'm trying to filter using regex in mySQL.

The field is a text field and I want to find all that match 'MD' or similar ('M.D.', 'M. D.', 'DDS, M.D.' etc.).

I do not want to accept those that contain M and D as a part of another acronym (e.g., 'DMD'). However 'DMD, M.D.' I would want to find.

Apologies if this is a simple task - I read through some regex tutorials and couldn't figure this out! Thanks.


Update: With help from the suggestions I arrived at the following solution:

(\s|^)M\.?\s*D\.?

which works for all of my cases. The quotes in my questions were to indicate it was a string, they are not a part of the string.

1
  • Pete if your question is solved, please marke it as resolved Mar 6, 2015 at 14:44

3 Answers 3

1

You can use a regex like this:

\b(M\.?\s*D\.?|D\.?\s*D\.?\s*S\.?)

Working demo

enter image description here

2
  • Thanks for this. I used your solution, substituting \. with \\. and \b with [[:<:]] so that it works in mySQL; it becomes [[:<:]](M\\.?\s*D\\.?). It's working mostly, but it is still matching 'O.M.D.' and 'D.M.D.', which I don't want. Do you have any ideas? Mar 5, 2015 at 20:16
  • @PeteLudlow you can use this regex instead ^(M\.?\s*D\.?|D\.?\s*D\.?\s*S\.?)$ working demo. But you will lose DMD, M.D. Mar 5, 2015 at 20:24
1

If I have understood your requirement:

'([^'.]*[ ,]*M[. ]*D[. ]*)'

this looks for MD preceded by space comma or ' separated by 0 or more dots & spaces, followed by ' it matches all the contents between the '' marks

test: https://regex101.com/r/oV2kV8/2

1

In the end I found this solution works:

(\s|^)M\.?\s*D\.?(\s|$)

This allows for the 'MD' to be at the start or after another credential and to have spaces or periods or nothing between the letters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.