3

The format I would like to allow in my text boxes are comma delimited lists followed by a line break in between the comma delimited lists. Here is an example of what I want from the user:

1,2,3
1,2,4
1,2,5
1,2,6

So far I have limited the user using this ValidationExpression:

^([1-9][0-9]*[]*[ ]*,[ ]*)*[1-9][0-9]*$

However with that expression, the user is only able to enter one row of comma delimited numbers.

How can proceed to accept multiple rows by accepting line breaks?

justinpees
  • 420
  • 5
  • 20
  • 1
    Is your input matrix always 3x3 or does this change? And does it have to be symmetric? – cansik Aug 06 '17 at 15:32
  • It is not always 3x3 it could be any size. They will be symmetric as well. – justinpees Aug 06 '17 at 15:34
  • 1
    I think symmetric will be a problem, because regex is not good at counting (it is not able to count how many numbers are already gone). – cansik Aug 06 '17 at 15:44
  • How would I do it if I didn't need them to be symmetric? (I will probably allow asymmetric rows.) – justinpees Aug 06 '17 at 15:48
  • Possible duplicate of [C#, regular expressions : how to parse comma-separated values, where some values might be quoted strings themselves containing commas](https://stackoverflow.com/questions/1189416/c-regular-expressions-how-to-parse-comma-separated-values-where-some-values) – hardkoded Aug 06 '17 at 16:05
  • I would need to allow multiple digits. ex: 1,2,10 1,2,11 – justinpees Aug 06 '17 at 16:45
  • 1
    I'd try [`^(?:[1-9][0-9]*(?:[ \t]*,[ \t]*[1-9][0-9]*)+[ \t]*\r?\n?)*$`](http://www.regexstorm.net/tester?p=%5e%28%3f%3a%5b1-9%5d%5b0-9%5d*%28%3f%3a%5b+%5ct%5d*%2c%5b+%5ct%5d*%5b1-9%5d%5b0-9%5d*%29%2b%5b+%5ct%5d*%5cr%3f%5cn%3f%29*%24&i=1%2c2%2c3%0d%0a1%2c2%2c4%0d%0a1%2c2%2c5%0d%0a1%2c2%2c6) – bobble bubble Aug 06 '17 at 16:51
  • @bobblebubble That works out perfectly thanks so much! – justinpees Aug 06 '17 at 16:55
  • @bobblebubble Would it be possible to allow different delimiters? But limit the user to using only one type of delimiter throughout? allowed example: 1.2.3 1.3.4 1.3.5 not allowed example: 1.2.3 1,3,4 1.3.5 – justinpees Aug 06 '17 at 17:07
  • 1
    Yes for example by [capturing](http://www.regular-expressions.info/brackets.html) the delimiter and using a backreference to the capture group. Try like [`^(?=.*?([.,]))(?:[1-9][0-9]*(?:[ \t]*\1[ \t]*[1-9][0-9]*)+[ \t]*\r?\n?)*$`](http://www.regexstorm.net/tester?p=%5e%28%3f%3d.*%3f%28%5b.%2c%5d%29%29%28%3f%3a%5b1-9%5d%5b0-9%5d*%28%3f%3a%5b+%5ct%5d*%5c1%5b+%5ct%5d*%5b1-9%5d%5b0-9%5d*%29%2b%5b+%5ct%5d*%5cr%3f%5cn%3f%29*%24&i=1%2c2%2c3%0d%0a1%2c2%2c4%0d%0a1%2c2%2c5%0d%0a1%2c2%2c6) – bobble bubble Aug 06 '17 at 17:19
  • Wow I just tried the expression above. That works so well!!! Would I run into problems by adding tab or space to the backreference? @bobblebubble – justinpees Aug 06 '17 at 17:33
  • I tried space and it works fine, I'm unable to test tab using the regex tester. But would my back reference look like this to allow tab? ([. ,\t)] – justinpees Aug 06 '17 at 17:54
  • 1
    @justinpees you can add whatever delimiter you like. yes like `([., \t])` Btw the capturing group is placed inside a [lookahead](http://www.regular-expressions.info/lookaround.html), haven't mentioned that before. It is triggered only at start and looks for the first match of the placed character class which is hold in group 1 (first capturing group) accessible via `\1`. – bobble bubble Aug 06 '17 at 17:55
  • @bobblebubble thanks so much for your help again! I really appreciate it!!! – justinpees Aug 06 '17 at 17:58
  • 1
    you're welcome! one thing: if you use eg `[., \t]` on string like `1 , 2` with a space/tab before the comma/dot it won't work as desired because the delimiter captured would be the first space/tab. Try fix: [`^(?=.*?[ \t]*([., \t]))(?:[1-9][0-9]*(?:[ \t]*\1[ \t]*[1-9][0-9]*)+[ \t]*\r?\n?)+$`](http://www.regexstorm.net/tester?p=%5e%28%3f%3d.*%3f%5b+%5ct%5d*%28%5b.%2c+%5ct%5d%29%29%28%3f%3a%5b1-9%5d%5b0-9%5d*%28%3f%3a%5b+%5ct%5d*%5c1%5b+%5ct%5d*%5b1-9%5d%5b0-9%5d*%29%2b%5b+%5ct%5d*%5cr%3f%5cn%3f%29%2b%24&i=1+%2c2%2c3) – bobble bubble Aug 06 '17 at 20:02
  • @bobblebubble I'm using the last expression you listed and I'm having a couple of issues. It won't allow some rows to have single or multiple spaces at the beginning of some rows. (using space as the delimiter) Is there a fix for this? Also, if you want to post as an answer I'd be more than happy to give you credit for best answer. http://www.regexstorm.net/tester?p=%5e%28%3f%3d.%2A%3f%5b+%5ct%5d%2A%28%5b.%2c+%5ct%5d%29%29%28%3f%3a%5b1-9%5d%5b0-9%5d%2A%28%3f%3a%5b+%5ct%5d%2A%5c1%5b+%5ct%5d%2A%5b1-9%5d%5b0-9%5d%2A%29%2b%5b+%5ct%5d%2A%5cr%3f%5cn%3f%29%2b%24&i=1+%2c2%2c3 – justinpees Aug 10 '17 at 00:42
  • 1
    Try [`^(?=.*?\d[ \t]*([., \t]))(?:[ \t]*[1-9]\d*(?:[ \t]*\1[ \t]*[1-9]\d*)+[ \t]*\r?\n?)+$`](http://www.regexstorm.net/tester?p=%5e%28%3f%3d.*%3f%5cd%5b+%5ct%5d*%28%5b.%2c+%5ct%5d%29%29%28%3f%3a%5b+%5ct%5d*%5b1-9%5d%5cd*%28%3f%3a%5b+%5ct%5d*%5c1%5b+%5ct%5d*%5b1-9%5d%5cd*%29%2b%5b+%5ct%5d*%5cr%3f%5cn%3f%29%2b%24&i=++1+++%2c2%2c3%0d%0a+++1%2c2%2c4%0d%0a++1%2c2%2c5%0d%0a1+++%2c+2++%2c++6+%0d%0a) you're right of course, have overseen this issue. – bobble bubble Aug 10 '17 at 15:14
  • @justinpees great it helped. initial question didn't contain detail like variable delimiter, also the given answer is matching good, so I won't put another answer but am happy if you got it going! – bobble bubble Aug 10 '17 at 18:27

1 Answers1

2

It is possible to check if the input has the correct format. I would recommend to use groups and repeat them:

((\d+,)+\d+\n?)+

But to check if the matrix is symmetric you have to use something else then regex.

Check it out here: https://regex101.com/r/GqtOuQ/2/

If you want to be a bit more user friendly it is possible to allow as much horizontal spaces as the user wants to add between the number and comma. This can be done with he regex group \h which allows every whitespace except \n.

The regex code looks now a bit more messy:

((\h*\d+\h*,\h*)+\h*\d+\h*\n?\h*)+

Check this out here: https://regex101.com/r/GqtOuQ/3

Here is the version that should work with .NET:

(([ \t]*\d+[ \t]*,[ \t]*)+[ \t]*\d+[ \t]*\n?[ \t]*)+
cansik
  • 1,924
  • 4
  • 19
  • 39
  • Ok that seems to be working, but how would I be able to allow one space before and/or after the comma in case the users input is a bit messy? – justinpees Aug 06 '17 at 15:57
  • 1
    Personally I would recommend to allow as many spaces / tabs as the user wants. Your software is able to trim it afterwards. Check my updated answer. – cansik Aug 06 '17 at 16:06
  • I like your updated answer better, but my web form won't open in browser once I made the changes. I get the following error when trying to visit the URL: The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. I didn't have this problem with your first expression that I tried. – justinpees Aug 06 '17 at 16:15
  • 1
    @justinpees you'd probably need to use space (if sufficient) or together with tab `[ \t]` instead of `\h` which is not supported by .net regex flavor. – bobble bubble Aug 06 '17 at 16:25
  • Can you give me the whole expression please? I'm a beginner with only 2 hours of experience with regex. – justinpees Aug 06 '17 at 16:32
  • @cansik answer was the best I just don't know why his second expression won't work. It would be really nice to allow whitespaces anywhere. – justinpees Aug 06 '17 at 16:46
  • I've added a version that works for .NET too. In .NET there is no `\h` as @bobblebubble already mentioned. – cansik Aug 06 '17 at 17:04