I am trying to write a regular expression that matches only any of the following:
{string}
{string[]}
{string[6]}
where instead of 6 in the last line, there could be any positive integer. Also, wherever string appears in the above lines, there could be int.
Here is the regular expression I initially wrote : {(string|int)(([]|[[0-9]])?)}. It worked well but wouldn’t allow more than one digit within the square bracket. To overcome this problem, I modified it this way : {(string|int)(([]|[[0-9]*])?)}.
The modified regex seems to be having serious problems. It matches {string[][]}. Can you please tell me what causes it to match against this? Also, when I try to enclose [0-9] within paranthesis, I get an exception saying “too many )'s“. Why does this happen?
Can you please tell me how to write the regular expression that would satisfy my requirements?
Note : I am tryingthis in C#
You need to escape the special characters like {} and []:
You might need to use
[0-9]instead of \d depending on the engine you use.