我有三个运行良好的正则表达式:
(?<=drbd:(?!.*/dev/.*))[^,]+
/dev/drbd.+?(?=,)
(?<=phy:(?!.*drbd.*))[^,]+
我需要所有人都只在他们要匹配的行不是以“#”符号开头时才匹配;也就是说,该行不会被注释掉。
我所有的尝试都与行的开头相匹配,但是我希望这只是与模式不匹配的条件。
disk = [ 'phy:/dev/vg0/xpto,xvda,rw']
#disk = [ 'drbd:resource23,xvda,rw']
disk = [ 'drbd:resource66,xvda,rw'
我需要匹配/dev/vg0/xpto
和resource66
。留下resource23
。
此外,我正在为python脚本编写代码,如果有人可以给我提示如何使正则表达式或脚本以某种方式与文件中的所有“磁盘”匹配,我将不胜感激!我尝试添加一些标志,但没有成功。
谢谢!
答案 0 :(得分:0)
文件“ testfile.txt”数据:
# ./configure
checking for library containing clock_gettime... none required
checking for CLOCK_MONOTONIC... yes
checking for setitimer... yes
checking for library containing setitimer... none required
checking for strcasestr... yes
checking for library containing strcasestr... none required
checking for memmem... yes
checking for library containing memmem... none required
checking for getdelim... yes
checking for library containing getdelim... none required
checking for BSD sysctl... no
checking for POSIX Threads with ''... no
checking for POSIX Threads with '-mt'... no
checking for POSIX Threads with '-pthread'... yes
configure: creating ./config.status
config.status: creating config.mak.autogen
config.status: executing config.mak.autogen commands
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
#git --version
git version 1.8.3.1
# make
SUBDIR git-gui
MSGFMT po/pt_pt.msg make[1]: *** [po/pt_pt.msg] Error 127
make: *** [all] Error 2
答案 1 :(得分:0)
也许是这样?
import re
content = """disk = [ 'phy:/dev/vg0/xpto,xvda,rw']
#disk = [ 'drbd:resource23,xvda,rw']
disk = [ 'drbd:resource66,xvda,rw'"""
get_paths = re.compile(r'(?mi)^(?!#).+(?:phy:|drbd:)(.+?),').findall
paths = get_paths(content)
print(paths)
# ['/dev/vg0/xpto', 'resource66']