1

I have found a Regex that test if the text passed to a TextBox is an email.

If Regex.IsMatch(email.Text, "^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +  "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$") _
Then
// True
End If

I want to change this, so it will test if the text typed is just Numbers ?

How can I do that ?

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Wassim AZIRAR
  • 10,179
  • 36
  • 115
  • 165
  • 3
    A regex to test for just integers is "^[0-9]+$". Maybe I'm misunderstanding your question, but what does testing for just numbers have to do with testing for e-mail addresses? – DCNYAM Jun 22 '10 at 12:42
  • No, you are not. I found this solution, but I haven't understood it. – Wassim AZIRAR Jun 22 '10 at 12:48
  • 1
    ^ = Beginning of line1. [0-9] = A range of characters, in this case numbers, from zero to nine. + = Match at least 1, but could be more than one. $ = End of line. Therefore, the regex ^[0-9]+$ will match any whole line of text containing at least one digit, but will disallow any non-numeric characters. – DCNYAM Jun 22 '10 at 12:58

2 Answers2

3

If you want to make sure the text contains only digits use a simple ^\d+$ or ^\s*\d+\s*$ to allow some spaces at the beginning and end.

To allow negative numbers: ^-?\d+$ or ^[+-]?\d+$ to allow numbers like +12

For decimal numbers: ^[+-]?\d+(\.\d+)?$ (this will allow 0.54 but not .54)

This one will allow things like .54

^[+-]?(\d+(\.\d+)?|\.\d+)$
Amarghosh
  • 55,378
  • 11
  • 87
  • 119
0

You might look at this answer for a definitive treatment of parsing numbers with regular expressions.

Community
  • 1
  • 1
tchrist
  • 74,913
  • 28
  • 118
  • 169