Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, everyone. I am working on mvc vb application. In one of my models I have this property:

Public Property Ladder As String


I want to validate it with regex so to have two digits and comma expression like 1,23,45,67 with this condition: every two digits are between 1-95.

I am not familiar with regex can you help me?

What I have tried:

So far I found in internet the following regex: ^.[0-9,]*$ which gives true only if the expression consists of digits and commas but this is not what I want.
Posted
Updated 8-Feb-19 10:54am
v2

Don't try to do "value" comparisons - i.e. "every two digits are between 1 and 95" - because regular expressions aren't "value processors": they are text processors and while it is possible to do:
(1|2|3|4|5|6|7|8|9|10|11| ... |93|94|95)
it's messy and difficult to maintain - particularly when you want to validate a string as comma delimited so you need to include the "value recognition" bit twice.

Just recognising a comma delimited string of one or two digits is easy:
^\d{1,2}(,\d{1,2})*$
But ... checking the value is best done in your presentation language.
 
Share this answer
 
v2
Comments
Richard Deeming 12-Feb-19 12:59pm    
No need to list every number:
([1-9]|[1-8]\d|9[1-5])

:)
OriginalGriff 12-Feb-19 14:05pm    
See how easy it is to c**k this up? You missed "90":
([1-9]|[1-8]\d|9[0-5])
And try it: it matches the first digit and ignores the second ...
:laugh:
Richard Deeming 12-Feb-19 14:12pm    
Not with proper anchoring and allowing for more than one group:
^([1-9]|[1-8]\d|9[0-5])(,([1-9]|[1-8]\d|9[0-5]))*$

Sure, it's not the easiest thing to read. But it's still shorter than listing out all 95 numbers. :)
OriginalGriff 12-Feb-19 14:16pm    
Yeah, and then next week management says "can you make it 1 to 215, but ignore 77?" :laugh:
Do it in a presentation language, not a regex.
What I would do would be to use the Split() function and then try to convert the array elements into integers, and then check the resulting values.

My VB is not what it was and I do not have an IDE at the moment, so there may be syntax or other errors in this sample.
VB
Dim LadderValues as String[] = Ladder.Split(new Char [] {','});
For Each LadderElement As String In LadderValues
  Dim LadderValue as Integer = -1
  If Integer.TryParse(LadderElement, LadderValue) Then
    If ((LadderValue < 1 ) Or (LadderValue >95 )) Then
        ' Deal with value out of range
    End If
  Else
    ' This element is not an integer
  End If
 
Share this answer
 
RegEx is made to match patterns, match 1 or 2 digits is easy, but if you want to match from 0 to 95; it is possible, but more complicated.
It is often considered easier to match 1 or 2 digits, then have code to do specific checking.

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900