0

This is just an experiment not an actual thing I need to do

attempt /x:\[((\d),?)*\]/

String Examples {x:[1,2,3] y:[5,6,7]} {x:[99,32,67,57,83] y:[5,6,7]} Is it possible to parse out the values in the array x using a single regular expression. I have tried using capture groups but they only capture the last match?

Is this something than can be done with regular expressions or is it context free?

I've tried experimenting with lazy and greedy evaluation of quantifiers but had no luck. It's like I want it to be greedy for part of the string and lazy for others.

It's easy to do in 2 passes. First extract x and the extract the values. Does this mean it can be done in one pass? (My guess is not as you could use lazy quantifiers for one and greedy for the other)

patrick_corrigan
  • 809
  • 11
  • 24
  • In what environment? Are items of `x` always numbers? – CertainPerformance Aug 29 '19 at 13:35
  • From a theoretical point of view. Yes – patrick_corrigan Aug 29 '19 at 13:36
  • 1
    Environment *does* matter. In some it'd be easy, in others, impossible. `(?:x:\[|\G,)\K\d+` works, if the environment supports those. Still, better to parse the JSON, if this is JSON – CertainPerformance Aug 29 '19 at 13:39
  • 1
    It's not json as the key's aren't quoted. A parser would definitely be the way to go. I just want to know if it's possible from a theoretical point of view. I guess I want to know if this is a regular language – patrick_corrigan Aug 29 '19 at 13:47
  • 2
    The **specific example** you posted can be parsed with a regular language. If arrays are allowed to contain other arrays or records, or records allowed to contain more records, then you need a context-free parser. – Dour High Arch Aug 29 '19 at 16:05

0 Answers0