Click here to Skip to main content
14,298,458 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to create regular expression to parse a piece of code.

here is my code snipett
Method1(100,variable1)

@"^(?\Method1)\((?\d+),\s(?\w+)\)$"

from above expression i am extracting different data.

But i some cases there are chances to commented lines
like below
Method1(100,variable1) 'method1 is called here


so how what should i add in my above regular expression to ignore strings after the ')' close bracket
Posted
Updated 31-Jan-12 22:49pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can try the regular expression
'/\*([^*]|[\r\n]|(\*([^/]|[\r\n])))*\*/'
to ignore comments in the code
   
v2
Comments
arun_pk 1-Feb-12 4:26am
   
sorry i did not understand fully..
so end of my regular expression i should add
'/\*([^*]|[\r\n]|(\*([^/]|[\r\n])))*\*/' ???
Amir Mahfoozi 1-Feb-12 4:45am
   
That is for C multiline comments syntax, I think.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Here it is :

string pattern = @"^\s*Method1\s*\(\s*(\d+)\s*,\s*([^)]*)\)(\s*\'.*)?\s*$";
Regex regex = new Regex(pattern, RegexOptions.Multiline);

string input = "    Method1( 100, variable1)  'comm 23  ";

var matches = regex.Matches(input);
foreach (Match match in matches)
{
    Console.WriteLine(match.Value);
    foreach (Group group in match.Groups)
    {
        Console.WriteLine(" >" + group.Value);
    }
}


Groups are created by embracing parts of pattern with parentheses so manipulate groups so that fits your needs.

Hope it helps.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hi,

Here's a solution:
Regex.Match(objstring, @"(Method1)(\()(\d+)(\,)(\w+)(\))").Value
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 4

i used just * at the end of my expression instead of $

@"^(?\Method1)\((?\d+),\s(?\w+)\)*"
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100