Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only digits using regexp

Tags:

regex

mysql

I have in table column pnum_s

I need get only that rows, which value in column pnum_s is exactly 10 symbol and all these symbols are only digits

what query must write for this?

I am trying

SELECT * FROM mytable WHERE pnum_s REGEXP '^\d+$'

But this not returns 0 rows

like image 993
Oto Shavadze Avatar asked Jun 04 '13 15:06

Oto Shavadze


People also ask

What is the regex for only numbers?

To get a string contains only numbers (0-9) we use a regular expression (/^[0-9]+$/) which allows only numbers.

How do I extract numbers from a string in R?

In this method to extract numbers from character string vector, the user has to call the gsub() function which is one of the inbuilt function of R language, and pass the pattern for the first occurrence of the number in the given strings and the vector of the string as the parameter of this function and in return, this ...

Does regex work with numbers?

The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.


4 Answers

The pattern you are looking for is either '^[0-9]{10}$' or '^[[:digit:]]{10}$'.

Everything is in the manual.

like image 107
RandomSeed Avatar answered Nov 06 '22 23:11

RandomSeed


I think with Mysql you'll want something like this:

^[[:digit:]]{10}$
like image 20
danpaq Avatar answered Nov 06 '22 21:11

danpaq


Check out the reference page.

http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp

What you're looking for is this:

SELECT * FROM mytable WHERE pnum_s REGEXP '^[[:digit:]]{10}$';
like image 38
rollcona Avatar answered Nov 06 '22 21:11

rollcona


try this pattern:

'^[0-9]{10}$'
like image 28
Casimir et Hippolyte Avatar answered Nov 06 '22 23:11

Casimir et Hippolyte