正则表达式以获得以下模式

时间:2013-03-23 12:31:56

标签: 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');
}

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

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

function test2(c,a,d)

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

有道理吗?

2 个答案:

答案 0 :(得分: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
----------------------------------------------------------------------

答案 1 :(得分:0)

这个怎么样:

MatchCollection matches = Regex.Matches(YourText, @"\{[^}]*""hello"".\}", RegexOptions.Multiline);
foreach (Match match in matches)
{
    foreach (Capture capture in match.Captures)
    {
    Console.WriteLine(capture.Value);
}