Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Python读取和搜索文件中的字符串_Regex_Python 3.4 - Fatal编程技术网

Regex Python读取和搜索文件中的字符串

Regex Python读取和搜索文件中的字符串,regex,python-3.4,Regex,Python 3.4,我想编写一个脚本来读取apache.conf文件,并获取不同参数的“MaxClients”值、“Keepalive”值、“KeepAliveTimeout”值和“ServerLimit”值等。但如果该行以“#”值开头,则不应读取该值。我已经编写了如下的示例代码,但它并没有忽略#value,有人能帮我做到这一点吗,我只需要值 重新导入 #afile=open('apache.txt','r') #对于文件中的aline: #aline1=aline.rstrip() #ax=re.findall

我想编写一个脚本来读取apache.conf文件,并获取不同参数的“MaxClients”值、“Keepalive”值、“KeepAliveTimeout”值和“ServerLimit”值等。但如果该行以“#”值开头,则不应读取该值。我已经编写了如下的示例代码,但它并没有忽略#value,有人能帮我做到这一点吗,我只需要值

重新导入
#afile=open('apache.txt','r')
#对于文件中的aline:
#aline1=aline.rstrip()
#ax=re.findall('MaxClients',aline1)
#打印(ax)
以open('apache.txt','r')作为文件:
对于文件中的行:
match=re.search('MaxClients([^,]+'),第行)
如果匹配:

打印(匹配组(1))
更改
re.search
功能如下

match = re.search(r'^(?! *#).*MaxClients ([^,]+)', line )

(?!*#)
负向前看,它声明行的开头后面没有
#
符号。

由于您是StackOverflow新手,建议您阅读