简体   繁体   English

使用python中的正则表达式精确检索1位数字

[英]Retrieve exactly 1 digit using regular expression in python

I want to print only ages that are less than 10. In this string, only the value 1 should be printed. 我只想打印小于10的年龄。在此字符串中,只应打印值1。 Somehow, that is not happening. 不知何故,这没有发生。 I used the following codes (using regular expression python) 我使用以下代码(使用正则表达式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']

The output should be 1 and not all the digits as displayed above. 输出应为1,而不是上面显示的所有数字。

What am i doing wrong ? 我究竟做错了什么 ?

You are trying to match "any 1 number", but you want to match "any 1 number, not followed or preceded by another number". 您正在尝试匹配“任意1个数字”,但是您想要匹配“任意1个数字,而不是后面跟着另一个数字”。

One way to do that is to use lookarounds 一种方法是使用环视

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

Possible lookarounds: 可能的解决方法:

(?<!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

Maybe a simpler solution is to use a capturing group () . 也许更简单的解决方案是使用捕获组() if regex in findall has one capturing group, it will return list of matches withing the group instead of whole matches: 如果findall中的正则表达式有一个捕获组,它将返回与该组匹配的匹配项列表,而不是整个匹配项:

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

Also note that you can replace any 0-9 with \\d - character group of numbers 还要注意,您可以用\\d数字字符组替换任何0-9

Try this : 尝试这个 :

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

This also works : 这也可以:

re.findall('(?<!\S)\d(?![^\s.,?!])', s5)
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)

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

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