【问题标题】:avoid returning @users inside a Link, with regex php避免使用正则表达式 php 在链接中返回@users
【发布时间】:2023-01-24 03:49:47
【问题描述】:

我需要替换字符串中所有包含符号“@”的@users。

条件是:

  1. 不需要在链接内
  2. 是否需要在空格之后。

    例如

      @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-]+)/'

标签: php regex


【解决方案1】:

使用 php,您可以更新模式以仅匹配并且不匹配 http:// 后跟可选的非空白字符:

http://S+(*SKIP)(*F)|@K[w-]+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    • 2016-07-17
    • 2021-01-04
    • 2016-01-10
    • 2021-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多