如何替换 perl 正则表达式中的字符串

发布于 2024-12-28 16:08:24 字数 361 浏览 1 评论 0原文

有没有办法替换正则表达式内部的字符?

像这样:

find x | xargs perl -pi -e 's/(as dasd asd)/replace(" ","",$1)/'

来自OP的评论

code find x | xargs perl -pi -e 's/work_search=1\/ttype=2\/tag=(.*?)">(.*?)<\/a>/work\/\L$1\E\" rel=\"follow\">$2<\/a>/g'

在这种情况下,我希望 $1 的空格被 _ 替换

Is there a way to replace characters from inside the regex?

like so:

find x | xargs perl -pi -e 's/(as dasd asd)/replace(" ","",$1)/'

From OP's comment

code find x | xargs perl -pi -e 's/work_search=1\/ttype=2\/tag=(.*?)">(.*?)<\/a>/work\/\L$1\E\" rel=\"follow\">$2<\/a>/g'

in this case i want $1's spaces be replaced with _

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

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

发布评论

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

评论(2

悟红尘 2025-01-04 16:08:24

您可以使用嵌套替换:

$ echo 'foo bar baz' | perl -wpE's/(\w+ \w+ \w+)/ $1 =~ s# ##gr /e'
foobarbaz

请注意,/r 修饰符需要 perl v5.14。对于早期版本,请使用:

$ echo 'foo bar baz' | perl -wpE's/(\w+ \w+ \w+)/my $x=$1; $x=~s# ##g; $x/e'
foobarbaz

另请注意,您需要为内部替换使用不同的分隔符。如您所见,我使用了#

You can use a nested substitution:

$ echo 'foo bar baz' | perl -wpE's/(\w+ \w+ \w+)/ $1 =~ s# ##gr /e'
foobarbaz

Note that the /r modifier requires perl v5.14. For earlier versions, use:

$ echo 'foo bar baz' | perl -wpE's/(\w+ \w+ \w+)/my $x=$1; $x=~s# ##g; $x/e'
foobarbaz

Note also that you need to use a different delimiter for the inner substitution. I used #, as you can see.

动听の歌 2025-01-04 16:08:24

据我了解,您想删除空格。正确吗?

你可以这样做:

s/(as) (dasd) (asd)/$1$2$3/

As far as I understand, you want to remove the spaces. Is it correct?

You can do:

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