使用re.finditer生成迭代对象,没有return,单独测试regex代码是ok的
Using re.finditer to generate iterative object, but no return, the regex code is ok when testing separately
这是正则表达式代码
pattern="""
(?P<host>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})
(\ \-\ )
(?P<user_name>[a-z]{1,100}\d{4}|\-{1})
( \[)(?P<time>\d{2}\/[A-Za-z]{3}\/\d{4}\:\d{2}\:\d{2}\:\d{2}\ -\d{4})
(\] ")
(?P<request>.+)
(")
"""
for item in re.finditer(pattern,text,re.VERBOSE):
# We can get the dictionary returned for the item with .groupdict()
print(item.groupdict())
我使用 Jupyter Notebook 来 运行 这些代码。
测试文字为
146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622
197.109.77.178 - kertzmann3129 [21/Jun/2019:15:45:25 -0700] "DELETE /virtual/solutions/target/web+services HTTP/2.0" 203 26554
主要问题是您没有在模式中转义文字 space。当使用 re.X
/ re.VERBOSE
时,模式中的任何白色 space(在字符 class 之外)都被视为格式化的白色 space 并且不在结束。在 Python re
模式中,[ ]
将 总是 匹配文字 space,但这在其他语言风格中不能保证,所以在使用 re.X
类似标志编译的模式中匹配 space 的最佳方法是 转义 space.
除此之外,还有一些需要注意的地方:
{1}
总是多余的,去掉它
- 可以将重复的模式分组到非捕获组中,并使用适当的量词进行量化,例如
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
=> \d{1,3}(?:\.\d{1,3}){3}
- 在 [=15] 中无需转义
/
和 :
(模式中的任何位置)和 -
(在字符 class 之外时) =]正则表达式。
因此,您可以使用
pattern = r'''(?P<host>\d{1,3}(?:\.\d{1,3}){3})
(\ -\ )
(?P<user_name>[a-z]{1,100}\d{4}|-)
(\ \[)(?P<time>\d{2}/[A-Za-z]{3}/\d{4}:\d{2}:\d{2}:\d{2}\ -\d{4})
(\]\ ")
(?P<request>.+)
(")'''
见regex demo and the Python demo:
import re
text = '''146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622
197.109.77.178 - kertzmann3129 [21/Jun/2019:15:45:25 -0700] "DELETE /virtual/solutions/target/web+services HTTP/2.0" 203 26554'''
pattern = r'''(?P<host>\d{1,3}(?:\.\d{1,3}){3})
(\ -\ )
(?P<user_name>[a-z]{1,100}\d{4}|-)
(\ \[)(?P<time>\d{2}/[A-Za-z]{3}/\d{4}:\d{2}:\d{2}:\d{2}\ -\d{4})
(\]\ ")
(?P<request>.+)
(")'''
for item in re.finditer(pattern,text,re.VERBOSE):
print(item.groupdict()) # We can get the dictionary returned for the item with .groupdict()
输出:
{'host': '146.204.224.152', 'user_name': 'feest6811', 'time': '21/Jun/2019:15:45:24 -0700', 'request': 'POST /incentivize HTTP/1.1'}
{'host': '197.109.77.178', 'user_name': 'kertzmann3129', 'time': '21/Jun/2019:15:45:25 -0700', 'request': 'DELETE /virtual/solutions/target/web+services HTTP/2.0'}
这是正则表达式代码
pattern="""
(?P<host>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})
(\ \-\ )
(?P<user_name>[a-z]{1,100}\d{4}|\-{1})
( \[)(?P<time>\d{2}\/[A-Za-z]{3}\/\d{4}\:\d{2}\:\d{2}\:\d{2}\ -\d{4})
(\] ")
(?P<request>.+)
(")
"""
for item in re.finditer(pattern,text,re.VERBOSE):
# We can get the dictionary returned for the item with .groupdict()
print(item.groupdict())
我使用 Jupyter Notebook 来 运行 这些代码。
测试文字为
146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622
197.109.77.178 - kertzmann3129 [21/Jun/2019:15:45:25 -0700] "DELETE /virtual/solutions/target/web+services HTTP/2.0" 203 26554
主要问题是您没有在模式中转义文字 space。当使用 re.X
/ re.VERBOSE
时,模式中的任何白色 space(在字符 class 之外)都被视为格式化的白色 space 并且不在结束。在 Python re
模式中,[ ]
将 总是 匹配文字 space,但这在其他语言风格中不能保证,所以在使用 re.X
类似标志编译的模式中匹配 space 的最佳方法是 转义 space.
除此之外,还有一些需要注意的地方:
{1}
总是多余的,去掉它- 可以将重复的模式分组到非捕获组中,并使用适当的量词进行量化,例如
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}
=>\d{1,3}(?:\.\d{1,3}){3}
- 在 [=15] 中无需转义
/
和:
(模式中的任何位置)和-
(在字符 class 之外时) =]正则表达式。
因此,您可以使用
pattern = r'''(?P<host>\d{1,3}(?:\.\d{1,3}){3})
(\ -\ )
(?P<user_name>[a-z]{1,100}\d{4}|-)
(\ \[)(?P<time>\d{2}/[A-Za-z]{3}/\d{4}:\d{2}:\d{2}:\d{2}\ -\d{4})
(\]\ ")
(?P<request>.+)
(")'''
见regex demo and the Python demo:
import re
text = '''146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622
197.109.77.178 - kertzmann3129 [21/Jun/2019:15:45:25 -0700] "DELETE /virtual/solutions/target/web+services HTTP/2.0" 203 26554'''
pattern = r'''(?P<host>\d{1,3}(?:\.\d{1,3}){3})
(\ -\ )
(?P<user_name>[a-z]{1,100}\d{4}|-)
(\ \[)(?P<time>\d{2}/[A-Za-z]{3}/\d{4}:\d{2}:\d{2}:\d{2}\ -\d{4})
(\]\ ")
(?P<request>.+)
(")'''
for item in re.finditer(pattern,text,re.VERBOSE):
print(item.groupdict()) # We can get the dictionary returned for the item with .groupdict()
输出:
{'host': '146.204.224.152', 'user_name': 'feest6811', 'time': '21/Jun/2019:15:45:24 -0700', 'request': 'POST /incentivize HTTP/1.1'}
{'host': '197.109.77.178', 'user_name': 'kertzmann3129', 'time': '21/Jun/2019:15:45:25 -0700', 'request': 'DELETE /virtual/solutions/target/web+services HTTP/2.0'}