Link to home
Start Free TrialLog in
Avatar of JAMES
JAMES

asked on

What regex pattern do I need to extract paremeters?

Hi,

I need to extract a variable number of parameters from a string eg.

TEST1 ("james")
TEST2 ("James", 123, true)

Each set of parameters is surrounded by an open ( and close ) parenthesis and each string is surrounded by double-quotes ".  Everything else ie. numerics and booleans are left as above.

Can these be easily extracted using a regular expression, if so how?

Many thanks.

James.
SOLUTION
Avatar of rfgkev
rfgkev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JAMES
JAMES

ASKER

Have I got this right - wasnt sure how to deal with the \

string pattern = @"[a-zA-Z]?";
think u need \ before each " to regex escape them an extra \ to c# escape the \ an " also

string pattern = "\\\"[a-zA-Z]?\\\"";
you did get it right, but if I understand your question correctly, this regex will not do the job for you.
Are you ONLY after the string parameters? or dop you want them all? If you want them all, how do you want them? separated by comma?
Avatar of JAMES

ASKER

Good point Daniel,

It would actually prove useful to extract them all ie :-
---------------------------------------------
Example 1

TEST1 ("james")

Would return :-

TEST1
james
---------------------------------------------

Example 2

TEST2 ("James", 123, true)

would return :-

TEST2
James
123
true

Thanks.
I think you should do it in 2 steps:
1 - Match the parameters to extract them - with an expression like: \(.*?\)
2 - Split by comma (either with Regex.Split or with String.Split) so you get them in a string array, or if you just want them separated by lines, replace the comma with a \n
Avatar of JAMES

ASKER

A string array is what im after.  I have been playing and this pattern nearly works  except I actually get the double quotes returned as one of the matches!

string pattern = "[^\",()\r\n]*";

JAMES,
I just thought, better replace the 1st step, instead of match do a replace to get rid of everything that is not params, so all before ( and the ( and ) signs, so use a regex like : ^.*?\(|\) with replace to get rid of them, and then you will be left with the params alone, comma separated so you can just split by comma on step 2.
JAMES,
> A string array is what im after.  I have been playing and this pattern
> nearly works  except I actually get the double quotes returned as one
> of the matches!

> string pattern = "[^\",()\r\n]*";
tried that, but it doesn't seem to be working. not sure I follow it. give my previous post a try - I think that would do the job.
Avatar of JAMES

ASKER

this is :-

string text = "HELLO(\"this\",\"is a test\",123,true";
string pattern = "[^\",()\\r\\n]*";
MatchCollection matches = Regex.Matches(text, pattern);

It does seem to work except it's not the double-quotes it's seeing but the spaces.  How can i tell this magic object to ignore spaces?
Avatar of JAMES

ASKER

This pattern seems to do it :-

[^\",()]*[\\da-zA-Z]

ie.

string pattern = "[^\",()]*[\\da-zA-Z]";

I have to say it's bloody black-magic to me but it seems to work!

Does it make sense?
Honestly, this very regex beats me, and I can't see how it would do the job (I would imagine this would match the function name too, and also would leave out the numbers, which didn't seem to be your intention).
But if it works for you, then use it :)
Avatar of JAMES

ASKER

I does retains numbers!!

I used RegexBuddy to construct it and here is the info.

Patters = [^",()]*[\da-zA-Z]
Input test string = launch("james",123,"fred","james ian")
Output strings =

launch
james
123
fred
james ian

You think you're confused - try being in my head ;-)


Avatar of JAMES

ASKER

And here is the full explanation :-

[^",()]*[\da-zA-Z]

Match a single character NOT present in the list "",()" «[^",()]*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character present in the list below «[\da-zA-Z]»
   Match a single digit 0..9 «\d»
   A character in the range between "a" and "z" «a-z»
   A character in the range between "A" and "Z" «A-Z»


Created with RegexBuddy
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JAMES

ASKER

As it happens returning the function name is perfect for me!

I will award you the points anyway as you helped me along the way.

Cheers.
Avatar of JAMES

ASKER

ps.

Thinking on this I will split the points between the two of you (unless there are any objections).

Feeling generous ;-)