简体   繁体   中英

python regex not match last char

Im Trying to match numbers separated by / inside [], but the last char is not included, why is this? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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