I'm looking for a regex to match acronyms like N.A.S.A. but also N.A.S.A without ending point. This solution RegEx to match acronyms works but only for acronyms ending with '.'
Any idea to match 'N.A.S.A' AND 'N.A.S.A.' ?
I'm looking for a regex to match acronyms like N.A.S.A. but also N.A.S.A without ending point. This solution RegEx to match acronyms works but only for acronyms ending with '.'
Any idea to match 'N.A.S.A' AND 'N.A.S.A.' ?
The \b(?:[a-zA-Z]\.){2,}
solution repeats the pattern inside the non-capturing group 2 or more times. You need to make sure .
is not required at the end:
\b[a-zA-Z](?:\.[a-zA-Z])+\b
To also match the .
after the last letter add \.?
:
\b[a-zA-Z](?:\.[a-zA-Z])+\b\.?
See the regex demo
NOTE To match uppercase letters only, remove a-z
.
The pattern matches
\b
- leading word boundary[a-zA-Z]
- 1 ASCII letter(?:\.[a-zA-Z])+
- 1 or more (so, at least 2 letters will be required) repetitions of
\.
- a dot [a-zA-Z]
- 1 ASCII letter\b
- trailing word boundary \.?
- 1 or 0 .
chars.P.S.: To enable any Unicode letter support, replace [a-zA-Z]
with \p{L}
and [A-Z]
with \p{Lu}
.
Never mind ;)
I did it like this : \b([a-z0-9]\.){1,}[a-z0-9]?\b