正则表达式用于移除括号外的文本

huangapple go评论58阅读模式
英文:

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

演示与解释

英文:

Use this:

.*?(\([^)]*\))

and replace with $1

Demo & explanation

答案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:

.* (\(..\))

huangapple
  • 本文由 发表于 2020年1月4日 01:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583182.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定