Perl 忽略正则表达式替换的替换侧的空格
假设我有 $str = "onetwo"
。
我想编写一个忽略空格的 reg ex 替换命令(这使其更具可读性):
$str =~ s/
one
two
/
three
four
/x
这会生成 "\n Three\nfour\n" ,而不是
"thirdfour"
> (其中 \n
是换行符)。基本上, /x
选项会忽略替换匹配侧的空格,但不会忽略替换侧的空格。如何也忽略替换侧的空格?
Suppose I have $str = "onetwo"
.
I would like to write a reg ex substitution command that ignores whitespace (which makes it more readable):
$str =~ s/
one
two
/
three
four
/x
Instead of "threefour"
, this produces "\nthree\nfour\n"
(where \n
is a newline). Basically the /x
option ignores whitespace for the matching side of the substitution but not the replacement side. How can I ignore whitespace on the replacement side as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
s{...}{...}
基本上是s{...}{qq{...}}e
。如果您不需要qq{...}
,则需要将其替换为其他内容。甚至:
clean
的可能实现:s{...}{...}
is basicallys{...}{qq{...}}e
. If you don't wantqq{...}
, you'll need to replace it with something else.Or even:
A possible implementation of
clean
: