windows路径的Python正则表达式

2024-10-01 17:38:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有

d = re.search(r'c:\wng\Qmns\vin2_2012-12-13_RES',r'c:\wng\Qmns\vin2_2012-12-13_RES_1.xls').

它回来了没有。什么我在这里失踪了吗?它应该找到绳子知道吗?在


Tags: researchresxls绳子wngvin2qmns
3条回答

要检查一个字符串是否以另一个字符串开头,不需要regexp:

path = r'c:\wng\Qmns\vin2_2012-12-13_RES_1.xls'

if path.startswith(r'c:\wng\Qmns\vin2_2012-12-13_RES'):
    ...

您需要在匹配端进行双转义,但不需要在目标上:

>>> re.search(r'c:\\wng\\Qmns\\vin2_2012-12-13_RES',r'c:\wng\Qmns\vin2_2012-12-13_RES_1.xls')
<_sre.SRE_Match object at 0x105ba34a8>
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.search(r'c:\wng\Qmns\vin2_2012-12-13_RES',r'c:\wng\Qmns\vin2_2012-12-13_RES_1.xls')
>>> re.search(r'c:\\wng\\Qmns\\vin2_2012-12-13_RES',r'c:\wng\Qmns\vin2_2012-12-13_RES_1.xls')
<_sre.SRE_Match object at 0x7f9c2000bb90>
>>> 

相关问题 更多 >

    热门问题