0

I have the input string below:

[text1][text2][text3]...[textN]

and I want to apply the following validation rule using regular expression:

The ] and [ cannot be included in other [].

For example, the next input strings are not correct:

[test1][test2[][test3]
[test1][test2]][test3] 
[test1][test2[lol][test3]
[test1][test2]lol][test3] 

I need to validate the input string because I am going to split it on [] groups (again using regular expression).

gotqn
  • 42,737
  • 46
  • 157
  • 243
  • 2
    Does it have to use regular expressions? – Derek Apr 30 '15 at 14:31
  • 3
    I've done it before by counting, loop through the string, add 1 when you see [ and subtract 1 when you see ]. If the value is ever 2, its nested. It has other uses too, if you go through the whole string and the result isn't zero, then you have mismatched brackets. – Ron Beyer Apr 30 '15 at 14:32
  • @Derek I am doing this in the context of `T-SQL` using SQL CLR implementations of .net regular expression functions. It seems to be the fastest way to validate the input string there? – gotqn Apr 30 '15 at 14:32
  • Is any text allowed _outside_ square brackets? Do you want to check on that, too? – C.Evenhuis Apr 30 '15 at 14:33
  • And you might want to move that logic into c# or wherever the code is BEFORE you try to write it to the database. – Derek Apr 30 '15 at 14:34
  • Maybe something like `"\[[^\[\]]*\]*"` ? – mirosval Apr 30 '15 at 14:34
  • @C.Evenhuis Yes, but I can do it after the first check, with separate regular expression. – gotqn Apr 30 '15 at 14:35
  • Do you want to validate or get matches ? – Pedro Lobito Apr 30 '15 at 14:39

2 Answers2

4

If you really want a regexp here is a quick one :

^(\[[^\[\]]+\])*$

Works on your examples

The principle here is for each bracket pair (\[.*\])* to contain any text that does NOT contains a bracket [^\[\]]+

In case you need to be able to have [test1][test2][][test3] working change the + with an * to allow the empty string to match

Irwene
  • 2,807
  • 23
  • 48
2

This should do the trick:

^(\[\w*\])*$

It means

  • ^ start with
  • [ a [
  • \w* multiple word characters (\w matches [A-Za-z0-9_])
  • ] a ]
  • * multiple times
  • $ end of string
Irwene
  • 2,807
  • 23
  • 48
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • 3
    @Sidewinder94 Numbers will work, but punctuation other than underscore wouldn't. `\w` is `[A-Za-z0-9_]` – juharr Apr 30 '15 at 14:37