-1

I have problem similar to this issue We have custom syntax for inserting 'references', it uses double square brackets. In this brackets could be only number (either positive 1,2 or negative -2, -3).

Now the problem: we need to validate the string that contains this references. If string contains no references - it is valid. If string contains valid references - it is also valid. If at least one reference is not valid (like missing end brackets, no numerics inside) it is not valid.

I could easily create expression that match our syntax of references '[[-?[[0-9]]]' and extract them, but have no ideas how to convert it to RegExp for validation (to exclude missing end brackets, non numerics etc).

We want to use Data Annotation validation attributes in ASP.NET so that it supports C#/javascript validation at the same time out of the box. It is better to have one regex than two repeated .IndexOf solutions (which is generally not a problem to implement) on client/server side to validate.

Here is some example values: test string valid - valid

test string valid [[1]] [[-22]] - valid

[[0]] test string vali[[1]][[-22]] - valid

test[[ - invalid

test [[1[[2]]]] - invalid

test [[1a]] - invalid

test [[1]]]] - invalid

S. Hooley
  • 284
  • 3
  • 9
Yauhen.F
  • 2,382
  • 3
  • 19
  • 25
  • I believe you are looking or something like [`^[^\][]*(?:\[\[-?\d+]][^\][]*)*$`](https://regex101.com/r/1ngmWy/3). Works the same in JS and C#. Also matches an empty string - not sure it is a problem. – Wiktor Stribiżew May 26 '17 at 18:13
  • Thanks a lot. It almost works. Somehow it only fails on test string [1] , which was not in my original request, but should also be valid string as it is not related to our syntax – Yauhen.F May 29 '17 at 07:40
  • But it is enclosed with single brackets, it makes the question unclear. `^[^\][]*(?:(?:\[\[-?\d+]]|\[-?\d+])[^\][]*)*$` might work but I'm even less sure now what you need. – Wiktor Stribiżew May 29 '17 at 07:58
  • what I need is: if there is occurrence of [[ (double square bracket) anything that goes after it should be number (negative/positive) closed by accomplying ]]. No inner [[ syntax inside of another [[. All other text should be - normal text. With possibility to have single brackets [, values enclosed in single brackets etc [1]. So if there is occurence of [[ - start validating according our syntax until ]]. Otherwise - it could be any normal text. – Yauhen.F May 29 '17 at 08:43
  • It seems like you need a negating pattern (match everything but some pattern). Could you please formulate it? I suggest failing each string that contains `[[` that are not immediately followed with an optional `-`, 1+ digits and `]]`. See [`^(?!.*\[\[(?!-?\d+]]))`](https://regex101.com/r/1ngmWy/5). – Wiktor Stribiżew May 29 '17 at 20:38
  • Hey, any feedback? – Wiktor Stribiżew Jun 03 '17 at 16:06
  • If you solved the problem please share your findings. Else, please clarify. – Wiktor Stribiżew Jul 01 '17 at 19:20

2 Answers2

0

Something like \[\[\-?\d+\]\] is at least close to what you want, but it also matches test [[1[[2]]]], which you said is invalid. Can you clarify why that shouldn't match so that I can modify this regex accordingly?

Update: based on your clarification about not allowing nested brackets, try this: (?<!\[)(\[\[\-?\d+\]\])(?!\]).

S. Hooley
  • 284
  • 3
  • 9
  • Generally it is the same as I have written in my original text (just somehow my slashes was escaped by stack, you just replaced 0-9 by d+). 1. It shouldn't match because references could not be inside another. – Yauhen.F May 24 '17 at 21:31
0

Maybe this is not what you are looking for, but it works for all your cases:

string pattern = @"\[[?>\[[?<c>]|[^[]]+|\][?<-c>]]*[?[c][?!]]\]";
string testString = "[[0]] test string vali[1][[-22]]";
int count = testString.Split('[').Length - 1;
MatchCollection matches = Regex.Matches(testString,pattern);
Console.WriteLine("Matches {0}",matches.Count);
Console.WriteLine("Count {0}", count);
if(count == matches.Count){
      Console.WriteLine("Valid");
}
else{
      Console.WriteLine("Invalid");
}
Hackerman
  • 12,139
  • 2
  • 34
  • 45
  • Why do you have `IgnorePatternWhiteSpace`? That setting is only for commenting a pattern. It does nothing for the actual regex parsing of text. – ΩmegaMan May 24 '17 at 20:26
  • Thanks. But anyway for validity you compare split count with regex count. Ideally I want one regular expression that I could pass to RegularExpressionValidator and if text matches it is valid if not it is not. But more I am thinking about problem more it looks it will be very very complex and difficult to support. – Yauhen.F May 24 '17 at 21:28