简体   繁体   English

我怎么能告诉一个字符串是否只包含使用正则表达式的python中的数字和空格

[英]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: 我正在尝试在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 [^AZ][^az][^ 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 ]因此得到的东西与任何字符和特殊符号都不匹配 - 只留下数字和空格(如果我是正确的话)。

This code will check if the string only contains numbers and space character. 此代码将检查字符串是否仅包含数字和空格字符。

if re.match("^[0-9 ]+$", myString):
    print "Only numbers and Spaces"

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 你可以在那里试试

Well I'd propose something that does not use regex : 好吧,我提出一些不使用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. 如果你正在工作, lambda只是制作单线程功能的一种简单方法。 If we wrote it using a def , then it would look like this: 如果我们使用def编写它,那么它看起来像这样:

>>> def to_my_linking(number_sequence):
...     return all(var.isdigit() for var in number_sequence.split())
...     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断字符串是否只包含字母AND空格 - How can I tell if a string only contains letter AND spaces 如何检查一个字符串只包含数字和/在python中? - How to I check that a string contains only digits and / in python? 如何检查字符串是否至少包含 2 位数字且没有空格? - How to check if a string contains at least 2 digits and no spaces? Python 列表仅包含数字整数或字符串 - Python list contains only digits integer or string 如何从给定的字符串中只选择数字/数字并使用 python 正则表达式跳过文本? - How to select only numbers/digits from a given string and skip text using python regex? 如何计算 Python 中字符串的数字、字母、空格? - How to count digits, letters, spaces for a string in Python? 使用 python regex 删除除单词、数字和空格之外的所有内容 - Removing everything except words, digits and spaces using python regex 使用regex sub(Python)复制字符串中的数字 - Replication of digits in a string using regex sub (Python) 如何判断一个字符串是否包含有效的 Python 代码 - How to tell if a string contains valid Python code 我如何找到一个字符串,其中包含两个正则表达式之间的任意字符串,但python中的某个正则表达式除外? - How can I find a string that contains any between two regular expressions except for a certain regex in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM