2

I'm trying to form a regex that will match a string that "looks" like an array.

  • Must start with [, { or (
  • Must end with ], }, or )
  • Does not matter if start and end use same bracket (e.g. (....} is fine)
  • Items are separated by commas
  • All white-space types are ignored
  • Empty arrays must be accepted

What I came up with initially was

'/\s*[\[|\{|\(]\s*\w+\s*[,\s*\w+]*\s*[\]|\}|\)]\s*/'

Unfortunately this doesn't accept an empty array. So I tried another

'/\s*[\[|\{|\(][\s*\w+\s*]?[,\s*\w+]*\s*[\]|\}|\)]\s*/'`

This one allows the empty array but also allows an array that starts with a comma after the opening bracket (e.g. [, item, item]).

What I'm doing currently is using two different regexes and checking that one or the other matches. The other regex is similar to the first one I mentioned here but only allows zero or more whitespace characters between the open and closing markers.

2 Answers 2

3

I've used spaces and newlines below for clarity. They should be removed or use a regex option that ignores them. I find it easier to develop regexs this way.

[\[\{\(]
    \s*
    (
    |
        \w+\s*
        (,\s*\w+\s*)*
    )
[\]\}\)]

This has not been tested, but I hope it's very close.

Sign up to request clarification or add additional context in comments.

2 Comments

For some reason I let the fact that parens capture sub-patterns blind me to the fact that they could be used to list alternatives. Can square brackets be nested similarly or is that the reason for using parens instead?
@Matt in all regex systems that I am familiar with, [] are used only for ranges like [a-z0-9_]. They do not nest and do not group. () are used for captures and to modify operator order just like in arithmetic (1+2)*3 vs 1+2*3. () can be nested. The capturing part of a given pair of () can often be disabled, for example (?:expression) in perl.
1

Try treating an empty array as a special case and or it with the populated array regex you have already. Something like (untested):

'(?:[\({[]\s*[\)}\]]|/\s*[\[|\{|\(]\s*\w+\s*[,\s*\w+]*\s*[\]|\}|\)]\s*)/'

1 Comment

I'm having trouble understanding how this works. Can you provide some more explanation?

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.