Python : regex lookbehind 在单引号或双引号后得到单词
Python : regex lookbehind get word after single or double quotes
我有如下内容的文件。我正在尝试提取文件中“-x”旁边的单词,最后只需要获得 uniq 结果。作为其中的一部分,我尝试了下面的正则表达式,但在输出中只得到了单引号和双引号。当我只对双引号使用正则表达式时,我得到了结果。
文件内容
00 04 * * 2-6 testuser /get_results.sh -q -x 'igp_srm_m' -s 'yesterday' -e 'yesterday' -m '2048' -b >>'/var/log/process/srm-console.log' 2>&1
00 10 * * 2-6 testuser /get_results.sh -q -x 'igp_srm_m' -s 'yesterday' -e 'yesterday' -m '2048' -w '720' >>'/var/log/process/srm-console.log' 2>&1
00 08 * * 1-5 testuser /get_results.sh -q -x "igp_france" -s "today" -e "today" -m "90000" -b -z partA >>"/var/log/process/france-partA-console.log" 2>&1
00 12 * * 2-6 testuser /get_results.sh -q -x "igp_france" -s "yesterday" -e "yesterday" -m "90000" -w "900" -z partA >>"/var/log/process/france-partA-console.log" 2>&1
00 08 * * 1-5 testuser /get_results.sh -q -x "igp_france" -s "today" -e "today" -m "90000" -b -z partB >>"/var/log/process/france-partB-console.log" 2>&1
00 12 * * 2-6 testuser /get_results.sh -q -x "igp_france" -s "yesterday" -e "yesterday" -m "90000" -w "900" -z partB >>"/var/log/process/france-partB-console.log" 2>&1
00 12 * * 2-6 testuser JAVA_OPTS='-server -Xmx512m' /merge.sh "yesterday" "igp_france" "partA,partB" >>"/var/log/process/france-console.log" 2>&1
00 08 * * 1-5 testuser /get_results.sh -q -x "igpswitz_france" -s "today" -e "today" -m "15000" -b >>'/var/log/process/igpswitz_france-console.log' 2>&1
00 12 * * 2-6 testuser /get_results.sh -q -x "igpswitz_france" -s "yesterday" -e "yesterday" -m "15000" -Dapc.maxalerts=8000 -w "900" >>'/var/log/process/igpswitz_france-console.log' 2>&1
30 07 * * 2-6 testuser /get_results.sh -q -x "igp_franced" -s 'yesterday' -e 'yesterday' -m "105000" -b >>"/var/log/process/franced-console.log" 2>&1
15 12 * * 2-6 testuser /get_results.sh -q -x "igp_franced" -s 'yesterday' -e 'yesterday' -m "105000" -w "960" >>"/var/log/process/franced-console.log" 2>&1
尝试语法
import re
with open ("test2") as file:
for line in file:
try:
m=re.search('(?<=\-x (\"|\'))(\w+)',line)
print m.group(1)
except:
m = None
预期输出
igp_srm_m
igp_france
igpswitz_france
igp_franced
收到输出
'
'
"
"
"
"
"
"
"
"
不确定出了什么问题,因为当我只尝试使用双引号时它工作正常。
仅适用于双引号的工作脚本
import re
with open ("test2") as file:
for line in file:
try:
m = re.search('(?<=\-x \")(\w*)', line)
print m.group(1)
except:
m = None
收到输出 - 仅搜索双引号
igp_france
igp_france
igp_france
igp_france
igpswitz_france
igpswitz_france
igp_franced
igp_franced
在
m=re.search('(?<=\-x (\"|\'))(\w+)',line)
print m.group(1)
代替组(1),使用组(2),
基本上,
m=re.search('(?<=\-x (\"|\'))(\w+)',line)
print m.group(2)
在 https://regex101.com/ 上试用后,第 1 组出现 '
,而使用第 2 组可提供所需的输出。
双引号工作正常,因为您需要的输出已经在第 1 组中。
您可以使用 set 来获取唯一值。
在您的模式中,值在第 2 组中,但您可以稍微优化一下模式。单引号和双引号可以用在字符 class (["'])
中并在第 1 组中捕获。然后您可以使用反向引用来配对匹配的引号 \
-x (["'])(\w+)
import re
result = set()
with open ("test2") as file:
for line in file:
try:
m = re.search(r"-x ([\"'])(\w+)", line)
result.add(m.group(2))
except:
m = None
print(result)
输出
{'igp_france', 'igp_srm_m', 'igp_franced', 'igpswitz_france'}
我有如下内容的文件。我正在尝试提取文件中“-x”旁边的单词,最后只需要获得 uniq 结果。作为其中的一部分,我尝试了下面的正则表达式,但在输出中只得到了单引号和双引号。当我只对双引号使用正则表达式时,我得到了结果。
文件内容
00 04 * * 2-6 testuser /get_results.sh -q -x 'igp_srm_m' -s 'yesterday' -e 'yesterday' -m '2048' -b >>'/var/log/process/srm-console.log' 2>&1
00 10 * * 2-6 testuser /get_results.sh -q -x 'igp_srm_m' -s 'yesterday' -e 'yesterday' -m '2048' -w '720' >>'/var/log/process/srm-console.log' 2>&1
00 08 * * 1-5 testuser /get_results.sh -q -x "igp_france" -s "today" -e "today" -m "90000" -b -z partA >>"/var/log/process/france-partA-console.log" 2>&1
00 12 * * 2-6 testuser /get_results.sh -q -x "igp_france" -s "yesterday" -e "yesterday" -m "90000" -w "900" -z partA >>"/var/log/process/france-partA-console.log" 2>&1
00 08 * * 1-5 testuser /get_results.sh -q -x "igp_france" -s "today" -e "today" -m "90000" -b -z partB >>"/var/log/process/france-partB-console.log" 2>&1
00 12 * * 2-6 testuser /get_results.sh -q -x "igp_france" -s "yesterday" -e "yesterday" -m "90000" -w "900" -z partB >>"/var/log/process/france-partB-console.log" 2>&1
00 12 * * 2-6 testuser JAVA_OPTS='-server -Xmx512m' /merge.sh "yesterday" "igp_france" "partA,partB" >>"/var/log/process/france-console.log" 2>&1
00 08 * * 1-5 testuser /get_results.sh -q -x "igpswitz_france" -s "today" -e "today" -m "15000" -b >>'/var/log/process/igpswitz_france-console.log' 2>&1
00 12 * * 2-6 testuser /get_results.sh -q -x "igpswitz_france" -s "yesterday" -e "yesterday" -m "15000" -Dapc.maxalerts=8000 -w "900" >>'/var/log/process/igpswitz_france-console.log' 2>&1
30 07 * * 2-6 testuser /get_results.sh -q -x "igp_franced" -s 'yesterday' -e 'yesterday' -m "105000" -b >>"/var/log/process/franced-console.log" 2>&1
15 12 * * 2-6 testuser /get_results.sh -q -x "igp_franced" -s 'yesterday' -e 'yesterday' -m "105000" -w "960" >>"/var/log/process/franced-console.log" 2>&1
尝试语法
import re
with open ("test2") as file:
for line in file:
try:
m=re.search('(?<=\-x (\"|\'))(\w+)',line)
print m.group(1)
except:
m = None
预期输出
igp_srm_m
igp_france
igpswitz_france
igp_franced
收到输出
'
'
"
"
"
"
"
"
"
"
不确定出了什么问题,因为当我只尝试使用双引号时它工作正常。
仅适用于双引号的工作脚本
import re
with open ("test2") as file:
for line in file:
try:
m = re.search('(?<=\-x \")(\w*)', line)
print m.group(1)
except:
m = None
收到输出 - 仅搜索双引号
igp_france
igp_france
igp_france
igp_france
igpswitz_france
igpswitz_france
igp_franced
igp_franced
在
m=re.search('(?<=\-x (\"|\'))(\w+)',line)
print m.group(1)
代替组(1),使用组(2), 基本上,
m=re.search('(?<=\-x (\"|\'))(\w+)',line)
print m.group(2)
在 https://regex101.com/ 上试用后,第 1 组出现 '
,而使用第 2 组可提供所需的输出。
双引号工作正常,因为您需要的输出已经在第 1 组中。
您可以使用 set 来获取唯一值。
在您的模式中,值在第 2 组中,但您可以稍微优化一下模式。单引号和双引号可以用在字符 class (["'])
中并在第 1 组中捕获。然后您可以使用反向引用来配对匹配的引号 \
-x (["'])(\w+)
import re
result = set()
with open ("test2") as file:
for line in file:
try:
m = re.search(r"-x ([\"'])(\w+)", line)
result.add(m.group(2))
except:
m = None
print(result)
输出
{'igp_france', 'igp_srm_m', 'igp_franced', 'igpswitz_france'}