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
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:
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]()
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
The "^[0-9]*$" is the expression we are using. the ^ indicates the beginning of the input, and the $ indicates the end of the input.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 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.


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!
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]()
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!

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
I think I might do some tutorials about regex :P
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!
I think you could turn this around and make it so numbers are not allowed. That'd also be good, nice tutorial.
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!
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.
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.
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
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023