简体   繁体   English

需要一个正则表达式

[英]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. C#, Regex 我需要正则表达式,需要提取括号括起来的任何字母数字值。 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)) -> 1 ((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();只需使用 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.或者,将LastIndexOfIndexOf以及"("")"Substring结合使用来确定值的位置。

\(([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.如果您还想检查每边的括号数量(问题中不清楚),正则表达式不是最佳选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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