使用python中的正则表达式检索正好1位数

时间:2018-04-04 04:51:17

标签: python regex

我想只打印小于10的年龄。在这个字符串中,只有 应打印值1。不知何故,这种情况并没有发生。 我使用了以下代码(使用正则表达式python)

import re

# This is my string

s5 = "The baby is 1 year old, Sri is 45 years old, Ann is 50 years old; 
their father, Sumo is 78 years old and their grandfather, Kris, is 100 years 
old"


# print all the single digits from the string
re.findall('[0-9]{1}', s5)
# Out[153]: ['1', '4', '5', '5', '0', '7', '8', '1', '0', '0']

re.findall('\d{1,1}', s5)
# Out[154]: ['1', '4', '5', '5', '0', '7', '8', '1', '0', '0']

re.findall('\d{1}', s5)
# Out[155]: ['1', '4', '5', '5', '0', '7', '8', '1', '0', '0']

输出应为1,而不是上面显示的所有数字。

我做错了什么?

3 个答案:

答案 0 :(得分:1)

您正在尝试匹配"任何1个号码",但您希望匹配"任何1个号码,未跟随或先于其他号码"。

一种方法是使用lookarounds

re.findall(r'(?<![0-9])[0-9](?![0-9])', s5)

可能的外观:

(?<!R)S   // negative lookbehind: match S that is not preceded by R
(?<=R)S   // positive lookbehind: match S that is preceded by R
(?!R)S   // negative lookahead: match S that is not followed by R
(?=R)S   // positive lookahead: match S that is followed by R

也许更简单的解决方案是使用捕获组()。如果findall中的正则表达式有一个捕获组,它将返回与该组匹配的匹配列表而不是整个匹配:

re.findall(r'[^0-9]([0-9])[^0-9]', s5)

另请注意,您可以将任何0-9替换为\d - 数字字符组

答案 1 :(得分:0)

试试这个:

k = re.findall('(?<!\S)\d(?!\S)', s5)
print(k)

这也有效:

re.findall('(?<!\S)\d(?![^\s.,?!])', s5)

答案 2 :(得分:0)

import re

s = "The baby is 1 year old, Sri is 45 years old, Ann is 50 years old; their father, Sumo is 78 years old and their grandfather, Kris, is 100 years old"

m = re.findall('\d+',s)


for i in m:
    if int(i)<10:
        print(i)