开发者

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

开发者 https://www.devze.com 2023-03-29 17:49 出处:网络
Is there a regex expression to find and replace any parentheses with two CAPITAL letters inside of it?

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

精彩评论

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