C#正则表达式匹配括号内的任何东西 [英] C# Regex match anything inside Parentheses
问题描述
我要匹配括号内任何东西,但结果必须排除括号为好。
例子:结果
初始化(P90W)结果
制动器(45X)
结果:结果
990W结果
45X
注意结果,而括号。
我一直在努力使这项工作,但无济于事我尝试了一些变化,但我知道这是一个简单的事情,我失踪,我不想使用替换来实现它去。
VAR项目=刹车(45X)
正则表达式搜索关键词=新的正则表达式(@\((*)\)。);
VAR值=(searchTerm.Match(项目).Groups.Count大于0)?
searchTerm.Match(项目).Groups [0] .value的:的String.Empty;
有人指责我使用零宽度断言所有的时间:
resultString = Regex.Match(subjectString,@(小于= \()+(?。? = \)))值。
但他们正是你想要的。不要捕捉你不想要捕获的内容。
I want to match anything inside parentheses but the result must exclude the parentheses as well.
Examples:
Initialize(P90W)
Brake(45X)
Result:
990W
45X
note results without the Parentheses.
I've been trying to make this work but to no avail I tried a few variations but I know it's a simple thing I'm missing and I don't want to go using Replace to achieve it.
var item = "Brake(45X)"
Regex searchTerm = new Regex(@"\((.*)\)");
var value = (searchTerm.Match(item).Groups.Count > 0) ?
searchTerm.Match(item).Groups[0].Value : string.Empty;
Some people accuse me of using zero width assertions all the time:
resultString = Regex.Match(subjectString, @"(?<=\().+?(?=\))").Value;
But they do exactly what you want. Don't capture what you don't want to capture.
这篇关于C#正则表达式匹配括号内的任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!