gpt4 book ai didi

Perl 正则表达式 : how to replace all desired character except of the first one

转载 作者:行者123 更新时间:2023-12-04 14:34:10 25 4
gpt4 key购买 nike

我有一个正则表达式,用 % 替换所有特殊字符(用于使用 LIKE 进行数据库搜索)。它看起来像这样:

$string =~ s/[^ a-zA-Z0-9]/%/g;

但是我不知道如何更改该表达式以替换字符串中第一个以外的所有所需特殊字符。所以如果我的字符串看起来像
"&Hi I'm smart(yeah right...)"

这将是
"&Hi I%m smart%yeah right%%%%"

(现在第一个 '&' 也被替换了)。

任何人都可以帮忙吗?

最佳答案

您可以使用 look-behind assertion至少需要一个字符:

s/(?<=.)[^ a-zA-Z0-9]/%/g;

关于Perl 正则表达式 : how to replace all desired character except of the first one,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5743577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com