简体   繁体   English

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

[英]Regular expression to get following pattern

Hi I have following line of code 嗨,我有以下代码行

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

I need to know Regex pattern which finds the above pattern for me, where string starts from { and then some text, then a keyword like "hello" and then }. 我需要知道正则表达式模式,该模式可以为我找到上述模式,其中字符串从{开始,然后是一些文本,然后是诸如“ hello”和}之类的关键字。

Please help 请帮忙

So i had 所以我有

function test(a,b,c){
}

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

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

which gives me function test(a,b,c) and 这给了我function test(a,b,c)

function test2(c,a,d)

I want a regular expression which gives me function when it finds a keyword like 'test' inside that function. 我想要一个正则表达式,当它在该函数中找到诸如“ test”之类的关键字时,该函数可以为我提供功能。

Makes sense? 说得通?

I don't know c# but the regex like (with . matching linefeed): 我不知道C#,但喜欢(与.匹配换行符)的正则表达式:

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

should do the job. 应该做的工作。

Explanation: 说明:

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
----------------------------------------------------------------------

How about this: 这个怎么样:

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