简体   繁体   中英

Regex to match all acronyms

I'm looking for a regex to match acronyms like NASA but also NASA without ending point. This solution RegEx to match acronyms works but only for acronyms ending with '.'

Any idea to match 'NASA' AND 'NASA' ?

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 az .

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.

PS : To enable any Unicode letter support, replace [a-zA-Z] with \\p{L} and [AZ] with \\p{Lu} .

没关系;) 我是这样做的: \\b([a-z0-9]\\.){1,}[a-z0-9]?\\b

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM