Page 1 of 1

Your first Regex tutorial - matching numbers only

Posted: Fri Apr 29, 2011 1:52 pm
by smashapps
Regular Expressions can be confusing for many people but when you actually get your head around it it isn't that bad I am still learning it myself but I want to show you guys something thats easy to do

Ok so what I am going to show you is I have a textbox and an error provider and a button.

If the textbox contains anything that ISNT a number then the error provider will tell you that you can only use numbers

when you press the button if you only used numbers then a msgbox will pop up saying it was only numbers

Code:
Code: Select all
Imports System.Text.RegularExpressions
Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim Numregex As New Regex("^[0-9]*$")
        If Numregex.IsMatch(TextBox1.Text) Then
            ErrorProvider1.SetError(TextBox1, [String].Empty)
            MsgBox("It only contained numbers, good")
        Else
            ErrorProvider1.SetError(TextBox1, "You can only use numbers")
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        Dim Numregex As New Regex("^[0-9]*$")
        If Numregex.IsMatch(TextBox1.Text) Then
            ErrorProvider1.SetError(TextBox1, [String].Empty)
        Else
            ErrorProvider1.SetError(TextBox1, "You can only use numbers")
        End If
    End Sub
End Class
The "^[0-9]*$" is the expression we are using. the ^ indicates the beginning of the input, and the $ indicates the end of the input.

the [0-9] is what it is looking for, and the * indicates that it can match any amount of numbers 0 to what ever number you want, for example it doesnt matter if you have 094096409640 or 358748574 as long as it is a number it is fine.

Image

Image

Re: Your first Regex tutorial - matching numbers only

Posted: Fri Apr 29, 2011 2:52 pm
by MrAksel
Really nice, i have never seen any tut like this earlier!

Re: Your first Regex tutorial - matching numbers only

Posted: Fri Apr 29, 2011 3:11 pm
by Axel
Don't forget to mention that [] matches every character and $ is the end of a string
I think I might do some tutorials about regex :P

Re: Your first Regex tutorial - matching numbers only

Posted: Fri Apr 29, 2011 10:51 pm
by smashapps
yes I mentioned $ was the end of the input and thanks

Re: Your first Regex tutorial - matching numbers only

Posted: Sat Apr 30, 2011 12:26 am
by 2cool4cereal2
I think you could turn this around and make it so numbers are not allowed. That'd also be good, nice tutorial.

Re: Your first Regex tutorial - matching numbers only

Posted: Sat Apr 30, 2011 12:35 am
by smashapps
Thanks and yes there is soo many things you can do with Regular expressions you can use them to find certain input patterns for example find a postcode or a phone number pretty cool

Re: Your first Regex tutorial - matching numbers only

Posted: Sun Dec 11, 2011 11:05 am
by renegade809
This is nice, but would you be able to do a tut that's a little more advanced?
I am trying to figure out how to use it with a WebBrowser Control. I suck at httpwebrequest, and I have only used Regex one time, to use it for scraping proxies.

Re: Your first Regex tutorial - matching numbers only

Posted: Sun Dec 11, 2011 11:50 am
by smashapps
I have stopped doing the regex tutorials sorry mate but you can find alot of great examples with a simple Google search =)

Hope you find what you need