-1

As I'm pretty new to Regular Expression, I'm looking for a regular expression which will validate whether entire string is comma separated & whether all the opened brackets/braces are closed.

Valid Cases:

(abc),(cde),(efg),(1234),[456]

(123),(345),(abcde),123,abc

edf,fgh,123,(abc),(123)

Invalid Cases:

(abc,cde,efg,123),345

[12344,adfc,(bcfgh),(123)

abcdef),123,(1234),(abc),(efgh)

abcdefgh;bcfg;123
Avinash Raj
  • 160,498
  • 22
  • 182
  • 229
Renju
  • 45
  • 5
  • http://stackoverflow.com/questions/546433/regular-expression-to-match-outer-brackets --> you need to look at this – dharmesh Sep 19 '16 at 08:13

1 Answers1

1

You can try something like this:

/^((?:\w+|\(\w+\)|\[\w+\])(,|$))+$/
Adam
  • 4,007
  • 1
  • 23
  • 54
  • This question got some really bad specs... Is `abc_123` supposed to pass? This lets it. Should `ABcd` pass? This lets it. – SamWhan Sep 19 '16 at 06:59
  • abc_123 - yes, ABcd -yes – Renju Sep 19 '16 at 08:46
  • Thank you @Adam for your time. One thing this Regex neglects is that empty spaced commas. i.e abc, ,1234, ,abc - this is a valid scenario. how will I include this scenario to the above RegEx? – Renju Sep 19 '16 at 09:38
  • `/^((?:\w+|\(\w+\)|\[\w+\]|\s+)(,|$))+$/` – Adam Sep 19 '16 at 09:55
  • Hi Adam thank you for your support. I have one more query posted regarding regex. Is it possible to have a regex in that pattern? @Adam (http://stackoverflow.com/questions/39592440/regular-expression-specific-to-a-particular-message-pattern-with-mandatory-eleme) – Renju Sep 21 '16 at 04:36