我想找到一个正则表达式来匹配句子中括号内的任何单词。匹配句子中括号内的任何单词
假设,我有一个句子。
"Welcome, (Hello, All of you) to the Stack Over flow."
说,如果我匹配的单词是Hello,
,All
,of
或you
。它应该返回true。 单词可能包含任何数字,符号但与其他空格分开
我试过这个\(([^)]*)\)
。但这个方法返回括号
static void Main(string[] args)
{
string ss = "Welcome, (Hello, All of you) to the Stack Over flow.";
Regex _regex = new Regex(@"\(([^)]*)\)");
Match match = _regex.Match(ss.ToLower());
if (match.Success)
{
ss = match.Groups[0].Value;
}
}
帮助和指导是非常赞赏封闭所有单词。
谢谢。
感谢人们为你准备的时间和答案。我终于通过将我的代码更改为Tim的回复来解决问题。 对于有类似问题的人。我在这里写我的最终代码
static void Main(string[] args)
{
string ss = "Welcome, (Hello, All of you) to the Stack Over flow.";
Regex _regex = new Regex(@"[^\s()]+(?=[^()]*\))");
Match match = _regex.Match(ss.ToLower());
while (match.Success)
{
ss = match.Groups[0].Value;
Console.WriteLine(ss);
match = match.NextMatch();
}
}
向我们展示您试过的代码。 – user3064914
更新了问题 – Miller
是否可以有(嵌套括号(如此)),如果是,那么这些词中的哪一个应该匹配? –