-4

I need help with regex, where I pass this kind of string:

"MethodName(int? Property1, string Property2, List<int?> Property3)"

and receive method and property names as string array. Something like this:

["MethodName","Property1","Property2","Property3"]

I've tried this: Regex to get method parameter name

and this Regex to extract function-name, & it's parameters

But could not get results I needed

Michael Samteladze
  • 1,310
  • 15
  • 38
  • I've updated my question, with what I've tried – Michael Samteladze Feb 27 '19 at 11:18
  • Using the second regex you provided works just fine. Simply split the second group on every `,` then split them all again on the `space` character and keep the last part to get the property name. – Zenoo Feb 27 '19 at 11:21

2 Answers2

4

You can achieve this using much simpler regex. Use this regex, which ensures it only matches method names or variable names by using look ahead to see what follows is optional space and either ( or , or )

\b\w+(?=\s*[,()])

Demo

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36
1

You can do something like this:

^(\w+)\((((.*)(\s)(.*)),((.*)(\s)(.*)),((.*)(\s)(.*)))\)

Keep in mind you have multiple groups.

https://regex101.com/r/2LDf6X/1

It's up to you to find a method to simplify this regex to catch variables parameters not only three.

As suggested by the user below, this is the correct and simplier regex:

\b\w+(?=\s*[,()])

Here a demo: https://regex101.com/r/WrG2kF/1

Dave
  • 1,912
  • 4
  • 16
  • 34
  • Thank you, it worked, but it fails if I do parameter less method string, like "MethodName()". Can you please show me the modification that would work for both scenarios? – Michael Samteladze Feb 27 '19 at 13:50
  • 1
    @MichaelSamteladze as suggested by PushpeshKumarRajwanshi this is the correct and simplier regex: \b\w+(?=\s*[,()]) – Dave Feb 28 '19 at 08:30