Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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 删除行末注释,除非引用_Regex_Perl_Comments - Fatal编程技术网

Regex 删除行末注释,除非引用

Regex 删除行末注释,除非引用,regex,perl,comments,Regex,Perl,Comments,我需要一个Perl正则表达式来删除行末注释。我觉得我在谷歌上搜索了一下,找不到合适的东西。详情如下: 下线注释用磅符号(#)表示 可以使用竖条(|)引用任何内容 因此,下面有一个评论: foo bar #baz 但以下情况并非如此: foo |quoted###with bars| 以下内容包含注释和包含注释字符的引号: foo |quoted###with bars| #comment here 我尝试的第一件事是s/#(?=[^ |]*$).$/,不幸的是,它删除了报价英镑。下一个不起

我需要一个Perl正则表达式来删除行末注释。我觉得我在谷歌上搜索了一下,找不到合适的东西。详情如下:

下线注释用磅符号(#)表示

可以使用竖条(|)引用任何内容

因此,下面有一个评论:

foo bar #baz
但以下情况并非如此:

foo |quoted###with bars|
以下内容包含注释和包含注释字符的引号:

foo |quoted###with bars| #comment here
我尝试的第一件事是
s/#(?=[^ |]*$).$/
,不幸的是,它删除了报价英镑。下一个不起作用的是
/#(?=[^ |]*$).$/
,因为它在多行引号上失败,如下所示:

foo |quote begins here ##still going
        ##and it's still going| #this is a quote, though.
我觉得我可以从中的正则表达式中为C/C++注释收集一些东西,但是它太复杂了,我无法只获取我需要的东西(不需要多行注释;)

任何人都可以提供一个删除EOL注释但忽略引用注释字符的正则表达式吗?

一种方法:

s/(\\\\\\\\\\\\\\\\\\\\\\\..*/$1\\\\\\\\\'''/eg


这会将
| | |
(包括
| |#| | | | |
)替换为自身,而
| |
几乎没有任何内容。

。。。不包括多行内容。@NateGlenn:当然包括;你试过了吗?当我在
abc | def#ghi
上试用时,加上一条新行,再加上
klm | nop#qrs
,它只剥离了
#qrs
,因为
#ghi
在一个多行
片段中。你看到不同的东西了吗?