【问题标题】:php request of a specific word but separable by spaces特定单词的php请求,但可以用空格分隔
【发布时间】:2017-04-17 19:42:23
【问题描述】:

我有一个关于正则表达式的问题,即适用于 php 的语法。 我知道一些关于 php 中正则表达式的基础知识,所以我知道我可以匹配一些东西

preg_match("/[maxmustermann ]/u", $input_line, $output_array);

现在我想匹配所有包含可选单词的文本,这些单词可以用空格分隔。

对不起,我只是不知道如何问。我试着举个例子。我有这个文本,想匹配所有的粗体。

orem ipsum dolor sit amet,consectetur ma​​x adipiscing elit。 Proin ma​​xm pellentesque dui ma​​xmustermann eu erat mustermann rhoncus tempor sit amet quis odio。 Max Mustermann 居民 morbi tristique senectus et netus et malesuada 成名 ac turpis egestas。 Max Muster Mann 和 malesuada 在 faucibus 中以 ac ante ipsum primis 闻名。 Nam vitae nisl dui。

这意味着我有这两个词:ma​​xmustermann,我想匹配包含 ma​​x 字母的单词(一个或多个) strong> 和/或 mustermann 按它们的顺序排列,但也是随机放置的空间。

谢谢

【问题讨论】:

  • maxmaxm 的匹配度如何?

标签: php regex


【解决方案1】:

您不能仅使用正则表达式来做到这一点。您需要首先提取由您选择的字母组成的所有单词,然后您必须过滤这些单词。像这样的:

$word = 'maxmustermann';
preg_match_all('~\b[aemnrstux]+\b~ui', $txt, $matches);

$result = array_filter($matches[0], function ($i) use ($word) {
    return stripos($word, $i) !== false;
});

demo

如果您想执行替换,您可以按照类似的方式进行:

$word = 'maxmustermann';
$result = preg_replace_callback('~\b[aemnrstux]+\b~ui', function ($m) use ($word) {
    return stripos($word, $m[0]) !== false ? "#{$m[0]}#" : $m[0];
}, $txt);

demo

【讨论】:

    【解决方案2】:

    编辑更新:

    重新阅读问题后,这是一个修改后的答案。
    只需使用下面的正则表达式执行 preg_match_all。

    OP 标准:

    我想匹配所有包含可选单词的文本,这些单词可以被分隔 一个空间。 ...

    我想匹配包含 max 和/或 mustermann 字母的单词(一个或多个) 它们的顺序也是随机放置的空间。

    为此,您需要使用空白边界。
    所有项目子字符串都按替代单词边界排序。

    此正则表达式还将匹配一组由空格分隔的子字符串。

    (?i)(?<!\S)(?!\s)(?:m|\b)(?:a|\b)(?:x|\b)(?:m|\b)(?:u|\b)(?:s|\b)(?:t|\b)(?:e|\b)(?:r|\b)(?:m|\b)(?:a|\b)(?:n|\b)(?:n|\b)(?:\s+(?!\s)(?:m|\b)(?:a|\b)(?:x|\b)(?:m|\b)(?:u|\b)(?:s|\b)(?:t|\b)(?:e|\b)(?:r|\b)(?:m|\b)(?:a|\b)(?:n|\b)(?:n|\b))*(?!\S)

    101 demo

    基准测试

    Regex1:   (?i)(?<!\S)(?!\s)(?:m|\b)(?:a|\b)(?:x|\b)(?:m|\b)(?:u|\b)(?:s|\b)(?:t|\b)(?:e|\b)(?:r|\b)(?:m|\b)(?:a|\b)(?:n|\b)(?:n|\b)(?:\s+(?!\s)(?:m|\b)(?:a|\b)(?:x|\b)(?:m|\b)(?:u|\b)(?:s|\b)(?:t|\b)(?:e|\b)(?:r|\b)(?:m|\b)(?:a|\b)(?:n|\b)(?:n|\b))*(?!\S)
    Completed iterations:   50  /  50     ( x 1000 )
    Matches found per iteration:   6
    Elapsed Time:    10.42 s,   10421.84 ms,   10421843 µs
    

    解释

     (?i)                  # Case insensitive modifier
     (?<! \S )             # Whitespace boundary behind
     (?! \s )              # Insure one of the next substrings match
    
     (?: m | \b )
     (?: a | \b )
     (?: x | \b )
     (?: m | \b )
     (?: u | \b )
     (?: s | \b )
     (?: t | \b )
     (?: e | \b )
     (?: r | \b )
     (?: m | \b )
     (?: a | \b )
     (?: n | \b )
     (?: n | \b )
    
     (?:
          \s+                   # Optional space and more words
          (?! \s )              # Insure one of the next substrings match
          (?: m | \b )
          (?: a | \b )
          (?: x | \b )
          (?: m | \b )
          (?: u | \b )
          (?: s | \b )
          (?: t | \b )
          (?: e | \b )
          (?: r | \b )
          (?: m | \b )
          (?: a | \b )
          (?: n | \b )
          (?: n | \b )
     )*
     (?! \S )              # Whitespace boundary ahead
    

    【讨论】:

    • 感谢您对 sytanx 的解释,但它没有捕捉到 maxm 字。
    • @AllanKarlson - 添加了纯正则表达式解决方案。这可能是更快的方法(尽管无论它如何完成,任务本质上都很慢)。
    【解决方案3】:

    您希望使用此正则表达式进行不区分大小写的匹配:

    /(max)|(muster)|(mann)/i
    

    EXAMPLE

    编辑:感谢@AbraCadaver 指出您还需要preg_match_all()

    【讨论】:

    • 你还需要preg_match_all
    猜你喜欢
    • 1970-01-01
    • 2010-12-01
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 2012-07-28
    相关资源
    最近更新 更多