import re
print(re.search('\(\d{1,}\)', "b' 1. Population, 2016 (1)'"))我试图提取字符串中括号之间的数字(一个或多个)。以上代码显示了我尝试的解决方案。我在https://regex101.com/上检查了正则表达式,并期望代码返回True。但是,返回的值为None。有人能告诉我发生了什么吗?
发布于 2022-10-10 05:37:07
当前的regex模式只有在使其成为原始字符串时才有效:
inp = "b' 1. Population, 2016 (1)'"
nums = re.findall(r'\((\d{1,})\)', inp)
print(nums) # ['1']否则,您将不得不双转义模式中的\\d。
https://stackoverflow.com/questions/74010511
复制相似问题