Python re 的正则表达式问题
Regex Issue with Python re
正在尝试使用 python re 包查找具有特定模式的文件名。得到了一个小测试脚本,其中所有值都被硬编码了,但这不是正常使用:
#!/usr/bin/env python
try:
import re2 as re
except ImportError:
import re
filepath1 = "C:\Users\Administrator\AppData\Local\Tempce4ba2a605e22b8699eef874d075fb585d259ed6cade2e503e6dbf58020aa0.exe:Zone.Identifier"
filepath2 = "C:\Users\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier"
re_pattern = re.compile("C\:\\Users\\[^\\]*\\AppData\\Local\\Temp\\[^.]*\.exe\:Zone\.Identifier")
print "1: " + str(re_pattern.search(filepath1))
print "2: " + str(re_pattern.search(filepath2))
出于某种原因,这个 returns None 代表 1,匹配代表 2,但据我所知,它们应该是匹配的。可能只是一个愚蠢的错误,但如果有人能发现它那就太棒了。
基本上,模式应该与 %TEMP% 目录中具有区域 ID 的任何 .exe 匹配,无论用户名如何
对不起,我误解了你的问题。
import re
filepath1 = r"C:\Users\Administrator\AppData\Local\Tempce4ba2a605e22b8699eef874d075fb585d259ed6cade2e503e6dbf58020aa0.exe:Zone.Identifier"
filepath2 = r"C:\Users\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier"
print(re.search(r"C\:\Users\(.*)\AppData\Local\Temp\[a-zA-Z0-9]+\.exe\:Zone\.Identifier$", filepath1))
print(re.search(r"C\:\Users\(.*)\AppData\Local\Temp\[a-zA-Z0-9]+\.exe\:Zone\.Identifier$", filepath2))
输出:
<_sre.SRE_Match object at 0x03176AE0>
<_sre.SRE_Match object at 0x03176AE0>
请注意文件路径开头的原始字符串(r
)
问题是其中一个路径以 7
开头,如果您在控制台中尝试 </code>,您会看到它被解释为代码,因为您不是使用 <em>原始前缀</em> 作为文字。</p>
<pre><code>>>> print("")
<some garbage char, bell?>
>>> print(r"")
这说明您的正则表达式不适用于该特定路径(对于另一条路径,您是 "lucky" 因为您使用的是 Python 2 和 \
+任何上限char 不是一个特定的转义序列,所以它没有改变(在 python 3 中,\U
被解释!)
现在,对于路径,在这种简单的情况下,您可以使用 fnmatch
来匹配通配符而不是正则表达式:
import fnmatch
filepath1 = r"C:\Users\Administrator\AppData\Local\Tempce4ba2a605e22b8699eef874d075fb585d259ed6cade2e503e6dbf58020aa0.exe:Zone.Identifier"
filepath2 = r"C:\Users\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier"
filepath3 = r"C:\Urs\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier"
for f in (filepath1,filepath2,filepath3):
print(f,fnmatch.fnmatch(f,r"C:\Users\*\AppData\*\Temp\*.exe:Zone.Identifier"))
打印:
C:\Users\Administrator\AppData\Local\Tempce4ba2a605e22b8699eef874d075fb585d259ed6cade2e503e6dbf58020aa0.exe:Zone.Identifier True
C:\Users\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier True
C:\Urs\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier False
正在尝试使用 python re 包查找具有特定模式的文件名。得到了一个小测试脚本,其中所有值都被硬编码了,但这不是正常使用:
#!/usr/bin/env python
try:
import re2 as re
except ImportError:
import re
filepath1 = "C:\Users\Administrator\AppData\Local\Tempce4ba2a605e22b8699eef874d075fb585d259ed6cade2e503e6dbf58020aa0.exe:Zone.Identifier"
filepath2 = "C:\Users\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier"
re_pattern = re.compile("C\:\\Users\\[^\\]*\\AppData\\Local\\Temp\\[^.]*\.exe\:Zone\.Identifier")
print "1: " + str(re_pattern.search(filepath1))
print "2: " + str(re_pattern.search(filepath2))
出于某种原因,这个 returns None 代表 1,匹配代表 2,但据我所知,它们应该是匹配的。可能只是一个愚蠢的错误,但如果有人能发现它那就太棒了。
基本上,模式应该与 %TEMP% 目录中具有区域 ID 的任何 .exe 匹配,无论用户名如何
对不起,我误解了你的问题。
import re
filepath1 = r"C:\Users\Administrator\AppData\Local\Tempce4ba2a605e22b8699eef874d075fb585d259ed6cade2e503e6dbf58020aa0.exe:Zone.Identifier"
filepath2 = r"C:\Users\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier"
print(re.search(r"C\:\Users\(.*)\AppData\Local\Temp\[a-zA-Z0-9]+\.exe\:Zone\.Identifier$", filepath1))
print(re.search(r"C\:\Users\(.*)\AppData\Local\Temp\[a-zA-Z0-9]+\.exe\:Zone\.Identifier$", filepath2))
输出:
<_sre.SRE_Match object at 0x03176AE0>
<_sre.SRE_Match object at 0x03176AE0>
请注意文件路径开头的原始字符串(r
)
问题是其中一个路径以 7
开头,如果您在控制台中尝试 </code>,您会看到它被解释为代码,因为您不是使用 <em>原始前缀</em> 作为文字。</p>
<pre><code>>>> print("")
<some garbage char, bell?>
>>> print(r"")
这说明您的正则表达式不适用于该特定路径(对于另一条路径,您是 "lucky" 因为您使用的是 Python 2 和 \
+任何上限char 不是一个特定的转义序列,所以它没有改变(在 python 3 中,\U
被解释!)
现在,对于路径,在这种简单的情况下,您可以使用 fnmatch
来匹配通配符而不是正则表达式:
import fnmatch
filepath1 = r"C:\Users\Administrator\AppData\Local\Tempce4ba2a605e22b8699eef874d075fb585d259ed6cade2e503e6dbf58020aa0.exe:Zone.Identifier"
filepath2 = r"C:\Users\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier"
filepath3 = r"C:\Urs\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier"
for f in (filepath1,filepath2,filepath3):
print(f,fnmatch.fnmatch(f,r"C:\Users\*\AppData\*\Temp\*.exe:Zone.Identifier"))
打印:
C:\Users\Administrator\AppData\Local\Tempce4ba2a605e22b8699eef874d075fb585d259ed6cade2e503e6dbf58020aa0.exe:Zone.Identifier True
C:\Users\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier True
C:\Urs\Administrator\AppData\Local\Temp\svchost.exe:Zone.Identifier False