I'm trying to write some script in python (using regex) and I have the following problem:
我正在尝试在python中编写一些脚本(使用正则表达式),我有以下问题:
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).
这意味着它应该匹配字符串:“213”(一个数字),“123 432”(两个数字),甚至“123 432 543 3 235 34 56 456 456 234 324 24”(十二个数字)。
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).
如果有人知道包含所有特殊字符的类(例如!$#_ - ),那就太棒了(因为那时我可以做[^ AZ] [^ az] [^ special-class]因此得到的东西不匹配任何字符和特殊符号 - 只留下数字和空格(如果我是正确的)。
3 个解决方案
#1
4
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
你可以在那里试试
#2
9
This code will check if the string only contains numbers and space character.
此代码将检查字符串是否仅包含数字和空格字符。
if re.match("^[0-9 ]+$", myString):
print "Only numbers and Spaces"
#3
0
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:
如果你正在工作,lambda只是制作单行功能的一种简单方法。如果我们使用def编写它,那么它看起来像这样:
>>> def to_my_linking(number_sequence):
... return all(var.isdigit() for var in number_sequence.split())
...
#1
4
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
你可以在那里试试
#2
9
This code will check if the string only contains numbers and space character.
此代码将检查字符串是否仅包含数字和空格字符。
if re.match("^[0-9 ]+$", myString):
print "Only numbers and Spaces"
#3
0
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:
如果你正在工作,lambda只是制作单行功能的一种简单方法。如果我们使用def编写它,那么它看起来像这样:
>>> def to_my_linking(number_sequence):
... return all(var.isdigit() for var in number_sequence.split())
...