Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex for field that allows numbers and spaces

Tags:

regex

I want that the text field only accept numeric values and spaces. What is regular expression for this? I was trying with \d+$ Thanks

like image 754
Diboliya Avatar asked Dec 20 '12 13:12

Diboliya


People also ask

Are spaces allowed in regex?

Yes, also your regex will match if there are just spaces. My reply was to Neha choudary's comment. @Pierre Three years later -- I came across this question today, saw your comment; I use regex hero (regexhero.net) for testing regular expressions.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

What does \d mean in regex?

In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart. \d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ).

Does \w in regex include space?

\W means "non-word characters", the inverse of \w , so it will match spaces as well.


1 Answers

This one should work :

[0-9 ]+ 
like image 51
Magus Avatar answered Oct 02 '22 12:10

Magus