0

I created a Regex which allows to capture the parameters of a function.

The parameters are separated by the character ,

You can have the character , inside a parameter like 'lorem, impum'

The regex returns only the last match.

My Regex :

\s*(\'.+\'|\".+\"|[^,]+)?(?:\s*,?\s*(\'.+\'|\".+\"|[^,]+))*

function parametes

111111111111,'222222222',"33333333333333"

LInk to regex exemple : https://regex101.com/r/fdRJ92/2

5
  • What is the desired output? Commented Mar 6, 2020 at 14:03
  • i need to catch only simple parameters stirng or number Commented Mar 6, 2020 at 14:16
  • the desired output is a list of all parameters, in this case 11,'test',"this is, a test" => [11, "test", "this is, a test"] Commented Mar 6, 2020 at 14:18
  • Good luck with 'I, don\'t like to be simple, you understand?' Commented Mar 6, 2020 at 14:20
  • "parameters" needs more definition. What do you accept as a parameter? You give as examples numbers and strings with either single or double quotes. What about escapes characters ("\"\\")? What about numbers with decimals (11.11)? What about variables (foo)? Variables with property accessors (foo.bar, baz["bat"])? Functions 11, foo("22"), 33? With enough complexity, you simply won't be able to achieve the desired result with regexes alone. You'll need a proper parser (like PegJS). You also haven't mentioned which language/API you are using (though I assume JS from the '/") Commented Mar 6, 2020 at 15:18

2 Answers 2

1
var rx = /(?:^|,)\s*((('|").*?(?<!\\)(\3))|(\d?\.?\d+))/g;
var str = `"21312\\'\\"3123\\"", 111111111111,'222222\\'222',"333333333,33333", 1234, .123, 0.3, '454'`;
var match;
while (match = rx.exec(str)) console.log(match[1]);

Output

"21312\'\"3123\""
111111111111
'222222\'222'
"333333333,33333"
1234
.123
0.3
'454'
Sign up to request clarification or add additional context in comments.

Comments

0

how about /\(?([^,\)]+)[\),]?/ :

  • matching an optional opening '(' (required to catch 1st parameter)
  • catch multiple characters that are NOT a (',' or ')' )
  • ending in either a ',' or a ')' (required for the last parameter)

Caution as this regex indeed assumes only simple strings or numbers. It will fail on the string as mentioned by @MonkeyZeus.

If commas are used IN an argument sentence, you're in trouble as a regex looks for tokens, not token context (separator or interpunction ?). You could try to exchange interpunction commas with a recognisable sequence, then split the arg list, then change the sequence back to a comma.

Example at https://regex101.com/r/xTkocI/1

2 Comments

Fails for foo, "bar, baz", bat and 11, bar(22, 33), 44
Yes - that's why I wrote the last paragraph ... . And 11, bar(22, 33), 44 does not correspond to the request of simple strings and numbers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.