如何使用 Perl 删除块注释?

发布于 2024-07-24 09:57:11 字数 266 浏览 10 评论 0原文

我正在开发一个分析 DSL 的预处理器。 我的目标是删除评论。 块注释工具前后由 %% 划分。 根据语言的定义,我不必担心 %% 出现在字符串中。

我正在使用这个 s/// 正则表达式。 不幸的是,它似乎匹配了一切并将其抹掉:

#Remove multiline comments.
$text_string =~ s/%%.*%%//msg;

我做错了什么?

I am working on a preprocessor that is analyzing a DSL. My goal is to remove the comments.
The block comment facility is demarcated by %% before and after. I do not have to worry about %% being in strings, by the definition of the language.

I am using this s/// regex. Unfortunately, it seems to match everything and wipe it out:

#Remove multiline comments.
$text_string =~ s/%%.*%%//msg;

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

三寸金莲 2024-07-31 09:57:11

你能做的第一件事就是让它变得非贪婪:

.*?

否则,

%%一些文字%%

真实内容

%%其他文本%%

将全部被擦除。

the first thing you can do is make it non-greedy:

.*?

otherwise,

%% some text %%

real content

%% other text %%

will all be wiped out.

撩起发的微风 2024-07-31 09:57:11

来自 perlfaq6:正则表达式贪婪意味着什么? 我该如何解决这个问题?


大多数人的意思是贪婪的正则表达式会尽可能匹配。 从技术上来说,实际上是量词(?、*、+、{})是贪婪的,而不是整个模式; Perl 更喜欢局部贪婪和即时满足,而不是整体贪婪。 要获取相同量词的非贪婪版本,请使用 (??, *?, +?, {}?)。

一个例子:

$s1 = $s2 = "I am very very cold";
$s1 =~ s/ve.*y //;      # I am cold
$s2 =~ s/ve.*?y //;     # I am very cold

注意第二个替换是如何在遇到“y”时立即停止匹配的。 这 *? 量词有效地告诉正则表达式引擎尽快找到匹配项,并将控制权传递给下一个队列,就像您在玩烫手山芋一样。

From perlfaq6: What does it mean that regexes are greedy? How can I get around it?


Most people mean that greedy regexes match as much as they can. Technically speaking, it's actually the quantifiers (?, *, +, {}) that are greedy rather than the whole pattern; Perl prefers local greed and immediate gratification to overall greed. To get non-greedy versions of the same quantifiers, use (??, *?, +?, {}?).

An example:

$s1 = $s2 = "I am very very cold";
$s1 =~ s/ve.*y //;      # I am cold
$s2 =~ s/ve.*?y //;     # I am very cold

Notice how the second substitution stopped matching as soon as it encountered "y ". The *? quantifier effectively tells the regular expression engine to find a match as quickly as possible and pass control on to whatever is next in line, like you would if you were playing hot potato.

断爱 2024-07-31 09:57:11

假设您已将整个代码读入变量 $str 并且在 %% 和 %% 之间不可能出现单个 %,您可以使用它。

$str =~ s/%%([^%]+)%%//g;

assuming that you have read entire code into the variable $str and between %% and %% there is no possibility of a single % occuring, you could use this.

$str =~ s/%%([^%]+)%%//g;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文