0

嗨,我有以下代码行

{
.....(here i can have anything, numbers, string special characters
...."hello"
}

我需要知道为我找到上述模式的正则表达式模式,其中字符串从 { 开始,然后是一些文本,然后是像“hello”这样的关键字,然后是 }。

请帮忙

所以我有

function test(a,b,c){
}

function test2(c,a,d){
if(a > 0){
    alert('test');
}

我得到了这个表达function\s+(\S+)\s*\((.|\n)*?\)\s*{

这给了我function test(a,b,c)

function test2(c,a,d)

我想要一个正则表达式,当它在该函数中找到像“test”这样的关键字时,它会给我函数。

说得通?

4

2 回答 2

1

我不知道 c# 但正则表达式喜欢(带有.匹配的换行符):

.*\bfunction\b.+?\bhello\b.+?\}

应该做的工作。

解释:

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
----------------------------------------------------------------------
于 2013-03-23T12:46:11.073 回答
0

这个怎么样:

MatchCollection matches = Regex.Matches(YourText, @"\{[^}]*""hello"".\}", RegexOptions.Multiline);
foreach (Match match in matches)
{
    foreach (Capture capture in match.Captures)
    {
    Console.WriteLine(capture.Value);
}
于 2013-03-23T13:02:05.890 回答