Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
python中的链式正则表达式_Python_Regex_Text Files - Fatal编程技术网

python中的链式正则表达式

python中的链式正则表达式,python,regex,text-files,Python,Regex,Text Files,您好,我目前正在尝试解析一个脚本,该脚本包含与下面给出的类似的文件路径。我想使用正则表达式解析文件,并将数据存储到文件之间以“\n”分隔的字符串中。下面给出了示例文件 SAMPLE FILE: ('#' is a comment would like to keep commented out) add file -tls "../path1/path2/path3/example_1.edf" add file -tls "../path1/path2/path3/ex

您好,我目前正在尝试解析一个脚本,该脚本包含与下面给出的类似的文件路径。我想使用正则表达式解析文件,并将数据存储到文件之间以“\n”分隔的字符串中。下面给出了示例文件

    SAMPLE FILE: ('#' is a comment would like to keep commented out)
    add file -tls "../path1/path2/path3/example_1.edf"
    add file -tls "../path1/path2/path3/example_1.v"
    add file -tls "../path1/path2/path3/exa_4mple_1.sv"
    add file -tls "../path1/path2/path3/example_1.vh"        
    #add file -tls "../path1/path2/path3/exa_0mple_1.vhd"

    SAMPLE OUTPUT: (this example excludes the '\n' character)
    example_1.v
    exa_4mple_1.sv
    example_1.vh
    #exa_0mple_1.vhd
如何构造扩展“re”,使其仅包含上述扩展,而不包括其他扩展?我还想知道是否有可能为注释掉的行捕获“#”,并在文件名前面加上“#”


不需要正则表达式:

>>> import os
>>> L = [
... "/path1/path2/path3/example_1.edf", 
... "/path1/path2/path3/example_1.v",
... "/path1/path2/path3/exa_4mple_1.sv", 
... "/path1/path2/path3/example_1.vh" ]
>>> for mypath in L:
...     if mypath.split('.')[-1] in ('v', 'sv', 'vh'):
...             print os.path.split(mypath)[1]
... 
example_1.v
exa_4mple_1.sv
example_1.vh
或作为列表:

>>> [os.path.split(mypath)[1] 
... for mypath in L 
... if mypath.split('.')[-1] in ('v', 'sv', 'vh')]
['example_1.v', 'exa_4mple_1.sv', 'example_1.vh']

这就是你想要的吗

import re

contents = '''
add file -tls "../path1/path2/path3/example_1.edf"
add file -tls "../path1/path2/path3/example_1.v"
add file -tls "../path1/path2/path3/exa_4mple_1.sv"     
add file -tls "../path1/path2/path3/example_1.vh"     
#add file -tls "../path1/path2/path3/exa_0mple_1.vhd"
'''

print contents

pat = "^(#?)add file.+?\"\.\./(?:\w+/)*(\w+?\.\w*v\w*)\"\s*$"

gen = (''.join(mat.groups())
       for mat in re.finditer(pat,contents,re.MULTILINE))

print '\n'.join(gen)
该模式允许捕获包含字母“v”的扩展名的路径,这是我从您的问题中理解的。
根据您的示例,我还将字符串
addfile
作为捕获的标准。
我在模式中使用了
\w

使用此模式,所有路径都应以
开头。/

如果所有这些特征都不适合您的情况,我们将更改需要更改的内容


请注意,我将
\s*
放在模式的末尾,以防路径后的行中有空格。

也许我的问题措词不当。变量contents用于一个非常大的脚本上的“contents=file_obj.read()”中,该脚本包含各种内部垃圾,这些垃圾与路径不同的文件混合在一起。我对路径格式设置的位置感到困惑。@mattcalis整个文件的内容就是路径吗?因为您可以执行类似于
file_obj.read().split('\n')
No的操作。该文件是一个非常大的脚本,内部有数千条路径,其中包含对其他脚本、工具、makefile组件等的调用@马特卡利斯,对不起,我不确定。你确定这是有效的代码吗?因为某些原因,我会出错!什么错误?如果你不说,我猜不出来你使用Python3吗?我使用的是2.7.5。我在re.finditer(pat,contents,re.MULTILINE)`)行的for mat上得到一个错误。它突出显示了多行后面的尾随(')。您真的需要其他人来找到纠正方法吗?在你看来,如果尾随(')被突出显示,那是为了表示什么?不管是什么,伙计,我用你的代码做了很多不同的尝试。我得到了我想要的原始代码。无论如何谢谢你的尝试。
import re

contents = '''
add file -tls "../path1/path2/path3/example_1.edf"
add file -tls "../path1/path2/path3/example_1.v"
add file -tls "../path1/path2/path3/exa_4mple_1.sv"     
add file -tls "../path1/path2/path3/example_1.vh"     
#add file -tls "../path1/path2/path3/exa_0mple_1.vhd"
'''

print contents

pat = "^(#?)add file.+?\"\.\./(?:\w+/)*(\w+?\.\w*v\w*)\"\s*$"

gen = (''.join(mat.groups())
       for mat in re.finditer(pat,contents,re.MULTILINE))

print '\n'.join(gen)