-3

I'm writing an interpreter and I'd like to capture the function arguments. Given something in quotes, like ('one, two',3,"four") I'd like to use regex (if its possible) to extract each of the elements: 'one, two', and 3, and "four" despite being single- or double- quoted, or no-quoted. I'd also like it to recognize to skip commas within quotes (so 'one, two' is interpreted as a single parameter). How is this possible?

I've tried some things and had no luck and haven't found any question like this yet that can handle quotes in the parameter list. I'd be grateful for any help.

schollz
  • 178
  • 8
  • 1
    Which programming langue? – January Jul 25 '19 at 14:36
  • https://stackoverflow.com/questions/2700953/a-regex-to-match-a-comma-that-isnt-surrounded-by-quotes?rq=1 and many other similar questions – Denys Séguret Jul 25 '19 at 14:39
  • Regex can be a convenient tool but when writing interpreters/parsers and the document does not conform to its syntax then regex will simply tell you "does not match". Depending on which level of error detection you want regex might be the wrong choice. – Burdui Jul 25 '19 at 15:43

1 Answers1

0

You can try this :'([^'()]*)'|"([^"()]*)"|\b(\S*?)\b\s*[,)]

This is a demo on regex101. https://regex101.com/r/3jkgWJ/1

Oliver Hao
  • 715
  • 3
  • 5