繁体   English   中英

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

[英]Regular expression to get following pattern

嗨,我有以下代码行

{
.....(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”之类的关键字时,该函数可以为我提供功能。

说得通?

我不知道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
----------------------------------------------------------------------

这个怎么样:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM