您好,我目前正在尝试解析一个脚本,其中包含类似于下面给出的文件的路径。我想使用正则表达式解析文件并将数据存储到一个字符串中,文件之间用 '\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”以使其仅包含上述扩展而排除其他扩展?我还想知道是否可以捕获注释掉的行的“#”,并在文件名前面加上“#”。
-Desired function:
for match in re.finditer(r'/([A-Za-z0-9_]+\..+)"', contents):
mylist.append(match.group(1))
-Working Code: ( tested on the '.v' file case )
re.finditer(r'/([A-Za-z0-9_]+\.v)"', contents)