okay
okay

Reputation: 173

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?

Upvotes: 0

Views: 192

Answers (2)

Toto
Toto

Reputation: 91518

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

Upvotes: 1

Dave Sexton
Dave Sexton

Reputation: 11188

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);
}

Upvotes: 0

Related Questions