Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用正则表达式查找几个不同的匹配项_Php_Regex - Fatal编程技术网

Php 使用正则表达式查找几个不同的匹配项

Php 使用正则表达式查找几个不同的匹配项,php,regex,Php,Regex,我正在检查是否有问题 “@username” “@username”, “@username” “@username.” “@username” “@username:” “@username:” 我目前有: $post->message = preg_replace( '/@ *('.preg_quote($speak['username'], '/').') *:/i', '[url=\''.PAGE_URL.RELATIVE_WBB_DIR.'/index.php?p

我正在检查是否有问题

  • “@username”
  • “@username”,
  • “@username”
  • “@username.”
  • “@username”
  • “@username:”
  • “@username:”
  • 我目前有:

    $post->message = preg_replace(
        '/@ *('.preg_quote($speak['username'], '/').') *:/i',
        '[url=\''.PAGE_URL.RELATIVE_WBB_DIR.'/index.php?page=User&userID='.$speak['toID'].'\']@'.$speak['username'].':[/url]',
        $post->message);
    

    有人知道我如何修改它以接受这些输入吗?

    修改您的正则表达式以添加标点符号:

    $post->message = preg_replace(
        '/@ *('.preg_quote($speak['username'], '/').')[:,.]? */i',
        '[url=\''.PAGE_URL.RELATIVE_WBB_DIR.'/index.php?page=User&userID='.$speak['toID'].'\']@'.$speak['username'].':[/url]',
        $post->message);
    
    请注意添加的部分:

    /@ *('.preg_quote($speak['username'], '/').')[:,.]? */i
                                                 ^^^^^^
    

    它可以选择匹配其中一个字符(冒号、逗号、句点)。

    下面是一个正则表达式,它应该可以实现您想要的:

    /^@[a-zA-Z]+[:\.\,]?[ ]?$/
    
    /^@         // string starts with '@'
    [a-zA-Z]+   // contains any letter at least once
    [:\.\,]?    // may contain any : , . zero or one time
    [ ]?$       // may contain a whitespace zero or one time at end of string
    

    你试过什么吗?这真的很简单。你已经在你原来的正则表达式中有了它,结尾是`*`