简体   繁体   中英

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

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

which gives me function test(a,b,c) and

function test2(c,a,d)

I want a regular expression which gives me function when it finds a keyword like 'test' inside that function.

Makes sense?

I don't know c# but the regex like (with . matching linefeed):

.*\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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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