英文:
RegEx to remove text not inside parentheses
问题
(AF)
(AX)
(AL)
(DZ)
(AS)
(AD)
(AO)
英文:
How can I remove text that is not in between parentheses? This Regex101 selects text inside of parentheses. I'm using BBEdit and the PCRE engine.
Convert:
AFGHANISTAN (AF)
LAND ISLANDS (AX)
ALBANIA (AL)
ALGERIA (DZ)
AMERICAN SAMOA (AS)
ANDORRA (AD)
ANGOLA (AO)
To:
(AF)
(AX)
(AL)
(DZ)
(AS)
(AD)
(AO)
答案1
得分: 3
使用以下正则表达式:
.*?(\([^)]*\))
替换为 $1
。
答案2
得分: 1
关于主权国家列表,除了大写字母之外,还应该出现“-”和“'”字符(例如“科特迪瓦”(也可能是“象牙海岸”)和“几内亚比绍”),因此不应该使用\w
。我建议使用稍微严格的正则表达式:
[A-Z'\- ]+ (\([A-Z'-]{2}\))
上面提到的一个宽松变种是这个:
.* (\(..\))
英文:
With regards to the List of sovereign states, aside of upper-case letters, there should also appear -
and '
characters (example "Cote d'Ivoire" (might also be "Ivory Coast") and "Guinea-Bissau") therefore \w
shall rather not be used. I'd go for a bit strict Regex:
[A-Z'\- ]+ (\([A-Z'-]{2}\))
A loose variant of the one above is this one:
.* (\(..\))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论