首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python : regex查找后获取单引号或双引号后面的单词

Python : regex查找后获取单引号或双引号后面的单词
EN

Stack Overflow用户
提问于 2020-12-08 06:57:38
回答 2查看 96关注 0票数 3

我有如下内容的文件。我试图提取文件中"-x“旁边的单词,最后只需要获得uniq结果。作为其中的一部分,我尝试了下面的regex,但是输出中只有单引号和双引号。当我只对双引号使用regex时,我得到了结果。

文件内容

代码语言:javascript
运行
复制
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

尝试语法

代码语言:javascript
运行
复制
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

预期输出

代码语言:javascript
运行
复制
igp_srm_m
igp_france
igpswitz_france
igp_franced

接收输出

代码语言:javascript
运行
复制
'
'
"
"
"
"
"
"
"
"

不知道出了什么问题,因为当我只尝试双引号时,它是正确的。

只适用于双引号的工作脚本

代码语言:javascript
运行
复制
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

接收输出-只搜索双引号()

代码语言:javascript
运行
复制
igp_france
igp_france
igp_france
igp_france
igpswitz_france
igpswitz_france
igp_franced
igp_franced
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-08 07:52:44

您可以使用设置获得唯一的值。

在您的模式中,值在第2组中,但是您可以对模式进行一些优化。单引号和双引号可以在字符类(["'])中使用,并在组1中捕获。

代码语言:javascript
运行
复制
-x (["'])(\w+)\1

Regex演示 x- Python演示

代码语言:javascript
运行
复制
import re

result = set()

with open ("test2") as file:
    for line in file:
        try:
            m = re.search(r"-x ([\"'])(\w+)\1", line)
            result.add(m.group(2))
        except:
            m = None

print(result)

输出

代码语言:javascript
运行
复制
{'igp_france', 'igp_srm_m', 'igp_franced', 'igpswitz_france'}
票数 4
EN

Stack Overflow用户

发布于 2020-12-08 07:15:52

在……里面

代码语言:javascript
运行
复制
m=re.search('(?<=\-x (\"|\'))(\w+)',line)
print m.group(1)

用组(2)代替组(1),基本上,

代码语言:javascript
运行
复制
m=re.search('(?<=\-x (\"|\'))(\w+)',line)
print m.group(2)

通过在https://regex101.com/上进行测试,第1组将成为',而使用组2则提供了所需的输出。

双引号之一工作正常,因为您所需的输出已经在第1组中。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65194406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档