简体   繁体   English

python正则表达式与最后一个字符不匹配

[英]python regex not match last char

Im Trying to match numbers separated by / inside [], but the last char is not included, why is this? Im试图匹配用[/]内的/分隔的数字,但不包括最后一个字符,这是为什么? but without the / 但没有/

import re
regex = r"\[.+?\]"
Sample1= "text text text[One]"
Sample2= "text text text[One/Two]"
Sample3= "text text text[One/Two/Three]"
lines=[Sample1,Sample2,Sample3]
print([re.findall(r"\[(.+?)[^\/]\]", s) for s in lines])

and the out put now is: 现在的输出是:

[['On'], ['One/Tw'], ['One/Two/Thre']]

and I wanted to be: 我想成为:

[['One'], ['One', 'Two'], ['One','Two','Three']]

What wold be the correct regex? 正确的正则表达式是什么?

matches = [re.findall(r"\[(.+[^\/])\]", s) for s in lines]
print(matches)

This will work. 这将起作用。 I have corrected the regex. 我已经纠正了正则表达式。

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

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