I am struggling to define a regular expression for matching a whole word that contains the substring .D. – such as found in Ph.D. or M.D., but also in other qualifications. The .D. may not always fall at the end of the word.
My poor starting attempt is:
[a-zA-Z\.]?\.D\.[a-zA-Z\.]?
But this fails completely to match either Ph.D. or M.D.
I’m using .NET regex but an example in another language that gives me a head start would be great.
It seems that you are confusing
?and*.*is what you need to be using since it can allow for more than 1 match (on the character set in[a-zA-Z\.]) to be found.[a-zA-Z\.]?will match 0 or 1 character only.[a-zA-Z\.]*will match 0-* characters.So the reason you weren’t getting a match on your first regex was because you had more than 1 qualifying characters in your string.