Results 1 to 11 of 11

Thread: [RESOLVED] Regular Expression Help Needed

  1. #1

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Resolved [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
    Last edited by wrack; Aug 18th, 2010 at 09:09 PM.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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:

    Code:
    \+*:*\d*(:*\d*)*

  3. #3

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Re: Regular Expression Help Needed

    Thanks.

    I tried that but it allows for "15:15:15" which is more than 1 ":" characters.

  4. #4

  5. #5

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    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?

  6. #6
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Regular Expression Help Needed

    Try this expression:
    vb Code:
    1. "^\+?(?::\d*|\d*:)?\d*"

  7. #7

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    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?

  8. #8
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    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

  9. #9

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    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.

  10. #10
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: Regular Expression Help Needed

    Try the regex I posted in post #6... or try this:
    vb Code:
    1. "\A(?:^\+?(?::\d*|\d*:)?\d*)\Z"

  11. #11

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Re: Regular Expression Help Needed

    Cool thanks. I got it all working.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width