Question

I want to extract some groups from command-like strings with the form:

  1. foo(bar,1). > need 'foo', 'bar' and '1'
  2. foo(bar,1.1). > need 'foo', 'bar' and '1.1'
  3. foo(bar-{foo,bar},1.1). > need 'foo', 'bar-{foo,bar}' and '1.1'

So a comma can be present in the first argument!

Since the second argument is always beginning with a digit, I could match for the first argument commas which are not followed by a number, but I just can't do it with lookahead :(

Here is my tries so far:

(\w+)\(([a-zA-Z0-9_\-\{\}~]+)(,(\d+(\.\d+)?))?\)\.

... but example 3 won't match this one.

(\w+)\(([a-zA-Z0-9_\-\{\}~\,(?!\d)]+)(,(\d+(\.\d+)?))?\)\.

... this one won't let me catch the groups > 2 (negative lookahead doesn't affect the first comma)

Here's the Debuggex example

Any help will be appreciated! Many thanks in advance :)

Was it helpful?

Solution

If {} are supposed to contain anyting then maybe this will work?

(\w+)\(([a-zA-Z0-9_\-~]+(\{.*\})?),(.+)\)\.

OTHER TIPS

This seems to do the trick:

(.*?)(?:\(|\)|,(?=\d))

Expl.: Match anything up to either a '(', ')' or a ',' followed by a digit.

If you want arbitrary spaces to be allowed:

\s*(.*?)\s*(?:\(|\)|,(?=\d))\s*

Use global flag to get all instances.

Regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top