如何使用 Perl 删除块注释?
我正在开发一个分析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你能做的第一件事就是让它变得非贪婪:
否则,
将全部被擦除。
the first thing you can do is make it non-greedy:
otherwise,
will all be wiped out.
来自 perlfaq6:正则表达式贪婪意味着什么? 我该如何解决这个问题?
大多数人的意思是贪婪的正则表达式会尽可能匹配。 从技术上来说,实际上是量词(?、*、+、{})是贪婪的,而不是整个模式; Perl 更喜欢局部贪婪和即时满足,而不是整体贪婪。 要获取相同量词的非贪婪版本,请使用 (??, *?, +?, {}?)。
一个例子:
注意第二个替换是如何在遇到“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:
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.
假设您已将整个代码读入变量 $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;