[英]How to use regex to remove string with only '11.x' pattern in it?
I have a list of values,我有一个值列表,
lst = ['python 3.x', 'java', 'pandas', '12.1.x/11.x', '7.x/8.x']
I want to replace the last two values with empty string ['12.1.x/11.x', '7.x/8.x']
我想用空字符串替换最后两个值
['12.1.x/11.x', '7.x/8.x']
How can I use regex to replace them with empty strings which generalizes across similar pattern?如何使用正则表达式将它们替换为跨相似模式泛化的空字符串?
pattern = re.sub('\d+\.x\/\d+\.x', '', lst[3]) # this is not working
You need to make sure to use anchors and specify that a second version number is optional:您需要确保使用锚点并指定第二个版本号是可选的:
^(?:\d+\.)+(?:x|\d+)(?:/(?:\d+\.)+(?:x|\d+))?$
https://regex101.com/r/c0CDr5/1 https://regex101.com/r/c0CDr5/1
I have no clue how to apply this correctly with Python so check out the Code Generator at the link above.我不知道如何使用 Python 正确应用此功能,因此请查看上面链接中的代码生成器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.