gpt4 book ai didi

php - 替换字符串中超过 6 位数字的正则表达式 php 中的单词

转载 作者:行者123 更新时间:2023-12-03 17:25:27 24 4
gpt4 key购买 nike

我想替换字符串中超过 6 位数字的所有单词。

示例:

'我的联系电话是(432)(323)(322)。我的另一个号码是+1239343。另一个是 343as32240'

致:

“我的联系电话已[已删除]”。我的另一个号码是[已删除]。另一个是[已删除]'

我知道正则表达式和 preg_replace。只需要正确的正则表达式即可。

最佳答案

您可以使用此正则表达式进行搜索:

(?<=\h|^)(?:[^\h\d]*\d){6}\S*

并替换为[已删除]

分手:

(?<=\h|^)      # loookbehind to assert previous position is line start or whitespace
(?: # start of non capturing group
[^\h\d]*\d # 0 or more non-space and non-digits followed by 1 digit
) # end of non capturing group
{6} # match 6 of this group
\S* # followed by 0 or more non-space characters

代码:

$result = preg_replace('/(?<=\h|^)(?:[^\h\d]*\d){6}\S*/', '[removed]', $str);

RegEx Demo

关于php - 替换字符串中超过 6 位数字的正则表达式 php 中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758148/

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