C#正则表达式匹配括号​​内的任何东西 [英] C# Regex match anything inside Parentheses

查看:757
本文介绍了C#正则表达式匹配括号​​内的任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要匹配括号内任何东西,但结果必须排除括号为好。



例子:结果
初始化(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆