RegEx忽略注释行。(RegEx Ignore a commented line.)

编程入门 行业动态 更新时间:2024-10-28 08:18:40
RegEx忽略注释行。(RegEx Ignore a commented line.)

我试图解析以下文本

# ---------------------------------------------------------------------------- # # Packages # ---------------------------------------------------------------------------- # set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_1_test_1_qip.vhd"] set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_2_test_1_qip.vhd"] set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_3_test_1_qip.vhd"] # Register Tool set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_INVALID_test_1_qip.vhd"] # ---------------------------------------------------------------------------- # # Sub Modules # ---------------------------------------------------------------------------- # set_global_assignment -name QIP_FILE [file join $::quartus(qip_path) "module_test_2.qip"] set_global_assignment -name QIP_FILE [file join $::quartus(qip_path) "module_test_3.qip"] # ---------------------------------------------------------------------------- # # Module Files # ---------------------------------------------------------------------------- # set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_4_test_1_qip.vhd"] set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_5_test_1_qip.vhd"]

使用正则表达式:

(?<=_FILE).*"(.+)"

这也工作正常,并给我上面的文本中的所有文件名,但它也给了我已注释掉的行的文件名。

我试图制作一个会忽略它的RegEx但是我无法让它工作。

这就是我尝试过的

(?<!#)(?:(?<=_FILE).+"(.+)")

请查看RegEx 101

关心Ephreal

I am trying to parse the following text

# ---------------------------------------------------------------------------- # # Packages # ---------------------------------------------------------------------------- # set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_1_test_1_qip.vhd"] set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_2_test_1_qip.vhd"] set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_3_test_1_qip.vhd"] # Register Tool set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_INVALID_test_1_qip.vhd"] # ---------------------------------------------------------------------------- # # Sub Modules # ---------------------------------------------------------------------------- # set_global_assignment -name QIP_FILE [file join $::quartus(qip_path) "module_test_2.qip"] set_global_assignment -name QIP_FILE [file join $::quartus(qip_path) "module_test_3.qip"] # ---------------------------------------------------------------------------- # # Module Files # ---------------------------------------------------------------------------- # set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_4_test_1_qip.vhd"] set_global_assignment -name VHDL_FILE [file join $::quartus(qip_path) "file_5_test_1_qip.vhd"]

Using the regex:

(?<=_FILE).*"(.+)"

This also works fine and gives me all the file names in text above, however it also gives me the file names of lines which have been commented out.

I've tried to make a RegEx which would ignore this however I cannot get it to work.

This is what I've tried

(?<!#)(?:(?<=_FILE).+"(.+)")

Please take a look at the RegEx 101

Regards Ephreal

最满意答案

要忽略注释行,您必须在行的开头匹配并匹配除#任何内容:

^[^#\n]*(?:(?<=_FILE).+"(.+)")

要不就

^[^#\n]*_FILE.+"(.+)"

两种模式都需要多线标志m 。

To ignore commented lines, you have to start matching at the start of the line and match anything but #:

^[^#\n]*(?:(?<=_FILE).+"(.+)")

Or just

^[^#\n]*_FILE.+"(.+)"

Both patterns need the multiline flag m.

更多推荐

本文发布于:2023-08-05 00:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   RegEx   Ignore   line   commented

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!