正则表达式匹配首字母缩略词 [英] RegEx to match acronyms
问题描述
我正在尝试编写一个正则表达式来匹配 U.S.
、D.C.
、U.S.A.
等值
I am trying to write a regular expression that will match values such as U.S.
, D.C.
, U.S.A.
, etc.
这是我目前所拥有的 -
Here is what I have so far -
\b([a-zA-Z]\.){2,}+
注意这个表达式如何匹配但不包括首字母缩略词中的最后一个字母.
Note how this expression matches but does not include the last letter in the acronym.
谁能帮忙解释一下我在这里遗漏了什么?
Can anyone help explain what I am missing here?
解决方案
我在这里发布解决方案,以防它对任何人有帮助.
I'm posting the solution here in case this helps anyone.
\b(?:[a-zA-Z]\.){2,}
这里好像需要一个非捕获组.
It seems as if a non-capturing group is required here.
推荐答案
Try (?:[a-zA-Z]\.){2,}
?:
(非捕获组)是因为您想省略捕获重复组的最后一次迭代.
?:
(non-capturing group) is there because you want to omit capturing the last iteration of the repeated group.
例如,没有 ?:
, 'U.S.A.'将产生您不感兴趣的小组赛A.".
For example, without ?:
, 'U.S.A.' will yield a group match 'A.', which you are not interested about.
这篇关于正则表达式匹配首字母缩略词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!