Your first Regex tutorial - matching numbers only

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
8 posts Page 1 of 1
Contributors
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

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
You do not have the required permissions to view the files attached to this post.
Last edited by smashapps on Sat Dec 12, 2015 10:49 am, edited 1 time in total.
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Really nice, i have never seen any tut like this earlier!
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

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
http://vagex.com/?ref=25000
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

yes I mentioned $ was the end of the input and thanks
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
2cool4cereal2
VIP - Donator
VIP - Donator
Posts: 151
Joined: Thu Oct 14, 2010 3:26 am

I think you could turn this around and make it so numbers are not allowed. That'd also be good, nice tutorial.
"I try to be modest at all times, and that's what makes me better than everyone else."
Image
Image
Image
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

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
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
renegade809
Just Registered
Just Registered
Posts: 5
Joined: Sun Dec 11, 2011 10:19 am

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.
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

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
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
8 posts Page 1 of 1
Return to “Tutorials”