简体   繁体   English

python regex,空格之间只有数字

[英]python regex, only digit between whitespaces

I have some strings and at some particular index i want to check if the next char is digit surrounded by one or more whitespaces. 我有一些字符串,并且在某些特定的索引下,我想检查下一个字符是否是被一个或多个空格包围的数字。

For example 例如

here is a string 这是一个字符串

'some data \\n 8 \\n more data' “一些数据\\ n 8 \\ n更多数据”

lets say i am iterating the string and currently standing at index 8, and at that position i want to know that if the next char is digit and only digit ignoring all the whitespaces before and after. 可以说我正在迭代字符串,当前位于索引8,在该位置,我想知道下一个字符是否为数字,并且只有数字忽略了前后的所有空格。

So, for the above particular case it should tell me True and for string like below 因此,对于上述特定情况,它应该告诉我True以及如下所示的字符串

'some data \\n (8 \\n more data' '一些数据\\ n(8 \\ n更多数据'

it should tell me False 它应该告诉我False

I tried the pattern below 我尝试了以下模式

r'\s*[0-9]+\s*'

but it doesn't work for me, may be i am using it incorrectly. 但它对我不起作用,可能是我使用不正确。

Try this: 尝试这个:

(?<=\s)[0-9]+(?=\s)

This regex uses a look-ahead and a look-behind, such that it matches the number only when the characters before and after it are whitespace characters. 此正则表达式使用先行查找和后向查找,以便仅在前后字符为空白字符时才与数字匹配。

In verbose form: 冗长的形式:

(?<=\s) # match if whitespace before
[0-9]+  # match digits
(?=\s)  # match if whitespace after

Your original regex didn't work because the "*" is saying "zero or more matches". 您的原始正则表达式无法正常工作,因为“ *”表示“零个或多个匹配项”。 Instead, you should use a "+", which means "one or more matches". 相反,您应该使用“ +”表示“一个或多个匹配项”。 See below: 见下文:

>>> import re
>>> s = 'some data \n 8 \n more data'
>>> if re.search("\s+[0-9]+\s+", s): print True
...
True
>>> s = 'some data \n 8) \n more data'
>>> if re.search("\s+[0-9]+\s+", s): print True
...
>>> s = 'some data \n 8343 \n more data'
>>> if re.search("\s+[0-9]+\s+", s): print True
...
True
>>>

If you just want to capture a single digit surrounded by one or more spaces, remove the "+" in front of "[0-9]" like this: 如果只想捕获由一个或多个空格包围的单个数字,请像这样删除“ [0-9]”前面的“ +”:

re.search("\s+[0-9]\s+", s)

Without regex: 没有正则表达式:

s1 = 'some data \n 8 \n more data'
s2 = 'some data \n (8 \n more data'

testString = lambda x: True if len(x.splitlines()[1].strip()) == 1 else False

print testString(s1)
print testString(s1)

Output: 输出:

True
False

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM