简体   繁体   中英

Need a Regular Expression for

All,

C#, Regex I need regular expression that requires to extract any alpha-numeric value that is surrounded by parentheses. Note about parentheses: There could be any number of parentheses on each side, but the number of parentheses on each side match (see Ex below).

Ex. Values (extract value of '1' from parentheses on each side):

(1) -> 1

((1)) -> 1

(((1))) -> 1

I have this expression but obviously its wrong and not sure how to preserve the value between (), etc.

\\(([^)]*)\\)

Just use Replace();

string myString = "(((1))) - 1";
myString = myString.Replace("(", "").Replace(")", "");

Do you have to use a regex?

As Victor suggested in the comment, why not simply

var number = someString.Replace("(", String.Empty).Replace(")", String.Empty);

Alternatively, use LastIndexOf and IndexOf and on "(" and ")" in conjunction with Substring to determine where the value is.

\(([0-9a-zA-Z]+)\)

Matches all alpha-numeric strings enclosed in single parentheses. If you want to also check the number of parentheses on each side (unclear from the question), regex isn't the best option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM