Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:Regex捕获genric用于3个案例。_Python_Regex_Findall - Fatal编程技术网

Python:Regex捕获genric用于3个案例。

Python:Regex捕获genric用于3个案例。,python,regex,findall,Python,Regex,Findall,嗨,有人帮我纠正一下我不工作的习惯吗 字符串大小写: 1) 120磅,适用于8岁及以上的骑手#捕获:8年及以上 2) 建议12岁及以上的爱好者使用56w x 28d x 32h英寸#第12条及以上 3) 4为有效使用而录制的用户语音语言导师吊舱尺寸为11l x 9w x 5h英寸,建议6岁及以上儿童使用#捕获:6及以上 我想要一个genric正则表达式,它可以完美地处理所有三个字符串 我的正则表达式是: \b\d+[\w+\s]?(?:\ban[a-z]\sup\b |\ban[a-z]\sab

嗨,有人帮我纠正一下我不工作的习惯吗

字符串大小写:

1) 120磅,适用于8岁及以上的骑手#捕获:8年及以上

2) 建议12岁及以上的爱好者使用56w x 28d x 32h英寸#第12条及以上

3) 4为有效使用而录制的用户语音语言导师吊舱尺寸为11l x 9w x 5h英寸,建议6岁及以上儿童使用#捕获:6及以上

我想要一个genric正则表达式,它可以完美地处理所有三个字符串

我的正则表达式是:

\b\d+[\w+\s]?(?:\ban[a-z]\sup\b |\ban[a-z]\sabove\b |\ban[a-z]\sall[a-z]*\b |\b&\sup)

但它并没有很好地发挥作用。如果有人能为我提供一个通用正则表达式,它对所有3种情况都有效我正在使用python re.findall()


有人吗?是否有帮助?

养成习惯,从详细的正则表达式开始:

import re
rx = re.compile(r'''
    ages\                                # look for ages
    (\d+(?:\ years)?\ and\ (?:above|up)) # capture a digit, years eventually
                                         # and one of above or up
''', re.VERBOSE)

string = '''
1) 120 lbs and is intended for riders ages 8 years and up. #catch : 8 years and up
2) 56w x 28d x 32h inches recommended for hobbyists recommended for ages 12 and up. #catch : 12 and up
3) 4 users recorded speech for effective use language tutor pod measures 11l x 9w x 5h inches recommended for ages 6 and above. #catch : 6 and above
'''

matches = rx.findall(string)
print(matches)
# ['8 years and up', '12 and up', '6 and above']

请同时参阅。

(由于我在评论中提出的建议似乎正是您想要的,因此我将其作为答案提供。)


如果您的示例演示了所有可能的字符串(但我担心它们没有),您可以尽可能简单地完成

\d+[^\d]*$

它匹配最后一个数字,以及后面的所有数字


或者更复杂一点-确保前面有年龄-

试试。希望这有帮助,
re.search(r'(?is)ages\s*(.*?)\.$),s)。group(1)
如果你的示例说明了所有可能的字符串(但我担心它们没有;),你可以做得像
\d+[^\d]*$
一样简单。它匹配最后一个数字及其后的所有内容。或者更复杂一点-确保前面有
age
-。实际上,根本不清楚什么通用模式适用于您,因为只有3个示例,没有提供规范。人们只能猜测你真正需要什么。\d+[^\d]*$是救世主,谢谢:-d.clasG。我不知道为什么这里的人会给出否定的观点。我有个问题要问。就这些。谢谢大家的回答。很好的一天。!