tags:

views:

111

answers:

4

I have a string in the format:

MyList.Where(abc).GroupBy(def).Sum(ghi)

The Where & GroupBy parts are optional as is the argument to the function, so.

ThisList.Count

is also valid.

I'm trying to find a RegEx string that will match this and return the values:
The List name (e.g. MyList), the where condition (e.g. abc), the GroupBy attribute (e.g def), the function name (e.g. Sum) and the function attribute (e.g. ghi).

Clarification:
The List name is one word
abc can be a complex expression, but there will be no close brackets which are not in quotation marks
def will be a single word
ghi will be a single word or a list of words separated by ","

+1  A: 
(?<list>\w+)\s*
([.]\s*Where\s*[(]\s*(?<wherearg>.*?)\s*[)]\s*)?
([.]\s*GroupBy\s*[(]\s*(?<groupbyarg>.*?)\s*[)]\s*)?
[.]\s*(?<func>\w+)\s*[(]\s*(?<funcarg>.*?)\s*[)]
František Žiačik
Nice! How would I make the brackets for the function argument optional?e.g.List.Count should work as well as List.Count()
Andrew White
Change last row like this: `[.]\s*(?<func>\w+)\s*([(]\s*(?<funcarg>.*?)\s*[)])?`
František Žiačik
+1  A: 

You can use a regex to validate the syntax, like:

^[A-Za-z]+(\.[A-Za-z]+\([A-Za-z]+\))*(\.[A-Za-z]+)?

but this will result in a valid or invalid decision.

I am not sure if you really want to grep the method names and values at the same time. That would limit your syntax: for instance all methods (GroupBy, Where) have to be in the right order. Maybe it is easier to split the string by . and extract the parts you need one by one.

tanascius
+1  A: 

Assuming where/groupby/function parameters to be words, you can use:

^(\w+)(?:\.Where\((\w+)\))?(?:\.GroupBy\((\w+)\))?\.(\w+)(?:\((\w+)\))?$
  • Group 1 would have the list name
  • Group 2 would have the where condition or null
  • Group 3 would have the group by condition or null
  • Group 4 would have the function
  • Group 5 would have the function parameter or null

If they're going to be complex C# expressions, the corresponding regex will be quite complex that you'll hate yourself tomorrow for choosing regex for this.

Amarghosh
A: 

This gives you the expected result.

^(?<List>[^\.]\*)|\.(?<Op>[^\(\.]*)\(*(?<Param>[^\)\.]*)
DHN