Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to create regular expression to parse a piece of code.

here is my code snipett
C#
Method1(100,variable1)

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

from above expression i am extracting different data.

But i some cases there are chances to commented lines
like below
VB
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

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

@"^(?\Method1)\((?\d+),\s(?\w+)\)*"
 
Share this answer
 
Hi,

Here's a solution:
C#
Regex.Match(objstring, @"(Method1)(\()(\d+)(\,)(\w+)(\))").Value
 
Share this answer
 
v3
Here it is :

C#
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.
 
Share this answer
 
You can try the regular expression
'/\*([^*]|[\r\n]|(\*([^/]|[\r\n])))*\*/'
to ignore comments in the code
 
Share this answer
 
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900