[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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.
0
JAMES
Asked:
JAMES
  • 9
  • 6
  • 2
2 Solutions
 
rfgkevCommented:
try

\"[a-zA-Z]?\"
0
 
JAMESAuthor Commented:
Have I got this right - wasnt sure how to deal with the \

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

string pattern = "\\\"[a-zA-Z]?\\\"";
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
daniel_ballaCommented:
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?
0
 
JAMESAuthor Commented:
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.
0
 
daniel_ballaCommented:
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
0
 
JAMESAuthor Commented:
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]*";

0
 
daniel_ballaCommented:
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.
0
 
daniel_ballaCommented:
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.
0
 
JAMESAuthor Commented:
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?
0
 
JAMESAuthor Commented:
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?
0
 
daniel_ballaCommented:
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 :)
0
 
JAMESAuthor Commented:
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 ;-)


0
 
JAMESAuthor Commented:
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
0
 
daniel_ballaCommented:
yes, fair enough. This one will do the job. Except that it will return the function name too, not just the parameters, but then again you will know it will always be the first one.
0
 
JAMESAuthor Commented:
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.
0
 
JAMESAuthor Commented:
ps.

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

Feeling generous ;-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 9
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now