【发布时间】:2023-01-24 03:49:47
【问题描述】:
我需要替换字符串中所有包含符号“@”的@users。
条件是:
- 不需要在链接内
- 是否需要在空格之后。
例如
@user1 follows other users @user2 and @user3在这种情况下,我使用下一个正则表达式并且效果很好
$regex = "/@+([a-zA-Z0-9-_]+)/"; $str = preg_replace($regex...当我添加一个链接时(tik tok 就像包含 @ 符号一样)...上面的正则表达式也返回“@dummy”
@user1 sent a link http://localhost/@dummy/video/7079513184146607365 to @user3为了修复此行为,我在正则表达式中的“@”符号前添加了一个“间隙”
$regex2 = "/ @+([a-zA-Z0-9-_]+)/"; $str = preg_replace($regex2...事实证明它返回“@user3”而不是“@user1”
在最后一个示例中,我可以使用什么正则表达式返回“@user1”、“@user3”而不是“@dummy”?
我尝试在“@”符号前加一个空格。
$regex2 = "/ @+([a-zA-Z0-9-_]+)/";我需要类似的东西(在“@”符号之前添加“[^/”)以避免返回链接内的所有@users
$regex2 = "/[^/@+([a-zA-Z0-9-_]+)/";
【问题讨论】:
-
也许只是
$regex = '/(?<!\S)@+([\w-]+)/'?