简体   繁体   中英

How to grab .// with regex python?

Why does this not work? In the regex finder, it matches. I'm trying to grab .// in strings

pat = '[\.\/]+(?!(docx|doc|pdf))'
bad = re.compile(pat)
bad.findall(tails[1])

print tails[1]
".//2005 Neuropathophys.doc"

This pattern seems to work on the regex matcher website http://regex101.com/

在此处输入图片说明

Your regex would be the below to match .// which is not followed by docx or doc or pdf ,

\.//(?!docx|doc|pdf)

DEMO

In this case I think you don't need \\ in the [] . I think \\ is used just as escape character. But you don't need to use \\ in [] in Python regex. Because characters are automatically escaped in the [] .

So use regexp [./]+ instead of [\\.\\/]+ .

sample:

>>> import re
>>> s = ".//2005 Neuropathophys.doc"
>>> re.match("[./]+", s).group()
'.//'

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