Regex pb

B

Bragadiru

Hi,

I'm using the following Regex to parse for method parameters. It works if
there are no spaces between commas. How can I change the regex to support
method calls like : MyMethod('uno', 'due','tres' , 'quatro' )
Regex(@"[^,\(\)]+",RegexOptions.Compiled); // Parse for function params
(...,...,...)


Thanks for any advice
 
G

Gadget

Hi,

I'm using the following Regex to parse for method parameters. It works if
there are no spaces between commas. How can I change the regex to support
method calls like : MyMethod('uno', 'due','tres' , 'quatro' )
Regex(@"[^,\(\)]+",RegexOptions.Compiled); // Parse for function params
(...,...,...)


Thanks for any advice

I'm sure some Regex guru can help you there (although it might be as simple
as something like [^ ] before and after your expression), but I was
wondering if you are trying to parse a language structure in general or
just a single line?
If you're trying to interpret your own language code then strongly consider
using the Gold parser at http://www.devincook.com/goldparser/ .
It's a bit heavy when you first look into it, but as it's similar to the
YACC and Boson type language creators it's very powerful and could save you
many days of coding in the long run.

Cheers,
Gadget
 
K

Kevin Yu [MSFT]

Hi Bragadiru,

To make sure that the arguments are in single quotes, you can add 2 single
quotes to get rid of the whitespace.

'[^,()]+'

Here is a useful too Regular Expression Workbench for you to test.

http://blogs.msdn.com/ericgu/archive/2003/07/07/52362.aspx

If you're parsing some .NET code, you can try to use CodeDom to achieve
this.

http://msdn2.microsoft.com/en-us/library/f1dfsbhc.aspx

If anything is unclear, please feel free to let me know.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
K

Kevin Spencer

Not knowing ALL of the rules you need to satisfy, I made up with some
assumptions of my own, and came up with the following:

(?<=\([\s]*|,[\s]*)['"]?(\w+)['"]?(?=[\s]*,|[\s]*\))

This Regular Expression is based on the following rules:

1. The match MUST be preceded either by:
a left parenthesis followed by 0 or more spaces, or
a comma followed by 0 or more spaces.
2. The match MUST be followed either by:
0 or more spaces, followed by a comma, or
0 or more spaces followed by a right parenthesis
3. The match MAY include one single or double quote at the beginning.
4. The match MAY include one single or double quote at the end.
5. The part inside the (optional) quotes is in Group 1.

This eliminates the Method Name (you didn't say you wanted it), and will
work with single or multiple parameters, with or without single or double
quotes. I tested it successfully against the following:

MyMethod('uno', 'due','tres' , 'quatro' )
MyOtherMethod(foo)

--
HTH,

Kevin Spencer
Microsoft MVP
Virtual Carpenter
http://unclechutney.blogspot.com

Paranoia is a state of mind.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top