C# 正则表达式以获得以下模式
嗨,我有下面一行代码C# 正则表达式以获得以下模式,c#,regex,C#,Regex,嗨,我有下面一行代码 { .....(here i can have anything, numbers, string special characters ...."hello" } 我需要知道Regex模式,它为我找到上面的模式,其中字符串从{开始,然后是一些文本,然后是一个关键字,比如“hello”,然后是} 请帮忙 所以我有 function test(a,b,c){ } function test2(c,a,d){ if(a > 0){ alert('test');
{
.....(here i can have anything, numbers, string special characters
...."hello"
}
我需要知道Regex模式,它为我找到上面的模式,其中字符串从{开始,然后是一些文本,然后是一个关键字,比如“hello”,然后是}
请帮忙
所以我有
function test(a,b,c){
}
function test2(c,a,d){
if(a > 0){
alert('test');
}
我得到了这个表达式function\s+(\s+)\s*\((.\124;\ n)*?\)\s*{
这给了我功能测试(a,b,c)
和
function test2(c,a,d)
我想要一个正则表达式,当它在函数中找到像“test”这样的关键字时,它会给我函数
有意义吗?我不知道c,但正则表达式类似于(与
匹配的换行符):
我应该做这项工作
说明:
The regular expression:
(?s-imx:.*\bfunction\b.+?\bhello\b.+?\})
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?s-imx: group, but do not capture (with . matching
\n) (case-sensitive) (with ^ and $ matching
normally) (matching whitespace and #
normally):
----------------------------------------------------------------------
.* any character (0 or more times (matching
the most amount possible))
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
function 'function'
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
.+? any character (1 or more times (matching
the least amount possible))
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
hello 'hello'
----------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
----------------------------------------------------------------------
.+? any character (1 or more times (matching
the least amount possible))
----------------------------------------------------------------------
\} '}'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
这个怎么样:
MatchCollection matches = Regex.Matches(YourText, @"\{[^}]*""hello"".\}", RegexOptions.Multiline);
foreach (Match match in matches)
{
foreach (Capture capture in match.Captures)
{
Console.WriteLine(capture.Value);
}
你能说得更具体一点,多发布一两个应该被解析的例子吗?好吧,让我发布一些现在没有任何意义的东西?更有意义,但这不是用正则表达式应该做的事情,尽管用C#的支持级别可能是可行的。我在C#中该怎么做?你能提供一些例子吗?
函数a(){“nothing here”};函数b(){“hello”}
将使正则表达式失败。我想你的意思是单行
,而不是多行
,这取决于你首先包含它的原因。
MatchCollection matches = Regex.Matches(YourText, @"\{[^}]*""hello"".\}", RegexOptions.Multiline);
foreach (Match match in matches)
{
foreach (Capture capture in match.Captures)
{
Console.WriteLine(capture.Value);
}