Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i tell if a string contains ONLY digits and spaces in python using regex

I'm trying to write some script in python (using regex) and I have the following problem:

I need to make sure that a string contains ONLY digits and spaces but it could contain any number of numbers.

Meaning it should match to the strings: "213" (one number), "123 432" (two numbers), and even "123 432 543 3 235 34 56 456 456 234 324 24" (twelve numbers).

I tried searching here but all I found is how to extract digits from strings (and the opposite) - and that's not helping me to make sure weather ALL OF THE STRING contains ONLY numbers (seperated by any amount of spaces).

Does anyone have a solution?

If someone knows of a class that contains all the special characters (such as ! $ # _ -) it would be excellent (because then I could just do [^A-Z][^a-z][^special-class] and therefore get something that don't match any characters and special symbols - leaving me only with digits and spaces (if I'm correct).

like image 755
Tomer Schweid Avatar asked Dec 12 '13 12:12

Tomer Schweid


People also ask

How do you check if a string contains only letters and numbers in Python?

Python isalnum In other words, isalnum() checks whether a string contains only letters or numbers or both. If all characters are alphanumeric, isalnum() returns the value True ; otherwise, the method returns the value False .

How do you check if a string contains only alphabets and spaces in Python?

Method #1 : Using all() + isspace() + isalpha() This is one of the way in which this task can be performed. In this, we compare the string for all elements being alphabets or space only.

How do you check if a string is just numbers Python?

The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False. Exponents, like ² and ¾ are also considered to be numeric values.

How do you check if a string is a number regex?

To check if a string contains only numbers in JavaScript, call the test() method on this regular expression: ^\d+$ . The test() method will return true if the string contains only numbers. Otherwise, it will return false . The RegExp test() method searches for a match between a regular expression and a string.


3 Answers

This code will check if the string only contains numbers and space character.

if re.match("^[0-9 ]+$", myString):
    print "Only numbers and Spaces"
like image 174
thefourtheye Avatar answered Sep 20 '22 15:09

thefourtheye


The basic regular expression would be

/^[0-9 ]+$/

However is works also if your string only contains space. To check there is at least one number you need the following

/^ *[0-9][0-9 ]*$/

You can try it on there

like image 22
mb14 Avatar answered Sep 22 '22 15:09

mb14


Well I'd propose something that does not use regex:

>>> s = "12a 123"
>>> to_my_linking = lambda x: all(var.isdigit() for var in x.split())
>>> to_my_linking(s)
False
>>> s = "1244 432"
>>> to_my_linking(s)
True
>>> a = "123 432 543 3 235 34 56 456 456 234 324 24"
>>> to_my_linking(a)
True

In case you're working the lambda is just a simple way to make a one liner function. If we wrote it using a def, then it would look like this:

>>> def to_my_linking(number_sequence):
...     return all(var.isdigit() for var in number_sequence.split())
...     
like image 29
Games Brainiac Avatar answered Sep 21 '22 15:09

Games Brainiac