开发者

Regex find and replace for (AB), (AC), (DD), (OT)

Is there a regex expression to find and replace any parentheses with two CAPITAL letters inside of it?

(AB)
(DD)
(OF)
(TO开发者_JAVA百科)


Try this to find two caps inside of parens:

\([A-Z]{2}\)

Replacing them depends on the technology you are using. In .NET you would look at the Regex.Replace method:

  string input = "some text";
  string pattern = @"\([A-Z]{2}\)";
  string replacement = "replace value";
  Regex rgx = new Regex(pattern);
  string result = rgx.Replace(input, replacement);


In ruby syntax (see this live example: http://rubular.com/r/udlIIqLCLC):

\([A-Z]{2}\)


In Perl syntax:

s/\([A-Z]{2}\)//g

This will replace any parentheses with two capital letters inside it with an empty string. If you want to replace them with some other string, just fill in the (empty) space between the last two slashes, e.g.

s/\([A-Z]{2}\)/replacement goes here/g


\([A-Z]{2}\)

will match your pattern.


Yes, and it's rather quite simple:

s/\([A-Z][A-Z]\)/replacement/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜