[RESOLVED] Regular Expression Help Needed
Hey guys,
I am in a need of a Regular Expression to restrict input to only certain characters in a text box. Any help would be gretly appreciated.
The text box can only allow 0 or 1 "+" sign at the beginning and at no other position.
The text box can only allow 0 or 1 ":" sign not before the "+" sign.
The text box can allow any number of 0-9 digits.
e.g.
10 = GOOD
+10 = GOOD
+:10 = GOOD
:10 = GOOD
12:20 = GOOD
+12:20 = GOOD
No other characters allowed.
Hope this makes sense.
TA :)
Re: Regular Expression Help Needed
Should work:
Code:
\+*:*\d{2}(:*\d{2})*
I assumed that only 2 digits in a row are allowed {2}
If you want any number of digits, i.e. 23432:2342342 then it would be:
Re: Regular Expression Help Needed
Thanks.
I tried that but it allows for "15:15:15" which is more than 1 ":" characters.
Re: Regular Expression Help Needed
Well, then try this:
Code:
\+*:*\d{2}(:*\d{2}){0,1}
Re: Regular Expression Help Needed
Thanks. But I am still having issues with it so finally managed to did it my way!
Anyways here it is with tiny errors.
Code:
\+{0,1}\d{1,3}|\+{0,1}:{0,1}\d{1,3}|\+{0,1}\d{1,2}:{0,1}\d{1,2}
The only problem is, it allows +7777 where I don't want the integer value to go beyond 3 characters so it should stop at + 777.
Can someone please help me?
Re: Regular Expression Help Needed
Re: Regular Expression Help Needed
Thanks Zach :)
For some reasons the input control I am using to restrict the user input is causing it to throw a syntex error! Even the though the RegEx parser knows it is correct.
Can you help me with the one I posted?
Re: Regular Expression Help Needed
Try this (replace TextBox1 with your textbox's name)
Code:
Try
If Regex.IsMatch(TextBox1.Text, "\A(?:^\+?(?::\d*|\d*:)?\d*)\Z") Then
' Successful match
Else
' Match attempt failed
End If
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try
Re: Regular Expression Help Needed
I would but the specific control I use, allows me to use RegEx as a mask to restrict user input which what I am after.
Re: Regular Expression Help Needed
Try the regex I posted in post #6... or try this:
vb Code:
"\A(?:^\+?(?::\d*|\d*:)?\d*)\Z"
Re: Regular Expression Help Needed
Cool thanks. I got it all working.