【问题标题】:Get next word after preg match with PHP与 PHP 预匹配后获取下一个单词
【发布时间】:2014-06-15 03:50:47
【问题描述】:

如何在与 PHP 进行预匹配后获取下一个单词。

例如,如果我有这样的字符串:

"This is a string, keyword next, some more text. keyword next-word."

我想使用preg_match 来获取“关键字”之后的下一个单词,包括该单词是否带有连字符。

所以在上面的例子中,我想返回“next”和“next-word”

我试过了:

$string = "This is a string, keyword next, some more text. keyword next-word.";

$keywords = preg_split("/(?<=\keyword\s)(\w+)/", $string);
print_r($keywords);

这只是返回所有内容,似乎根本不起作用。

非常感谢任何帮助。

【问题讨论】:

  • strpos 会告诉您出现的位置,并在之后轻松找到要点。
  • strpos 只返回第一次出现。我想把所有东西都推到一个数组中
  • 啊,很公平。不过,我的正则表达式经验不知道您提供的正则表达式的 &lt;= 部分。对不起。
  • @JeremyMiller FWIW,(?&lt;= 是积极回顾的一部分。因此,如果有人使用(?&lt;=a)b,则意味着b 仅在前面有a 时才匹配。更多信息:regular-expressions.info/lookaround.html

标签: php preg-match


【解决方案1】:

使用您的示例,这应该可以使用preg_match_all

// Set the test string.
$string = "This is a string, keyword next, some more text. keyword next-word. keyword another_word. Okay, keyword do-rae-mi-fa_so_la.";

// Set the regex.
$regex = '/(?<=\bkeyword\s)(?:[\w-]+)/is';

// Run the regex with preg_match_all.
preg_match_all($regex, $string, $matches);

// Dump the resulst for testing.
echo '<pre>';
print_r($matches);
echo '</pre>';

我得到的结果是:

Array
(
    [0] => Array
        (
            [0] => next
            [1] => next-word
            [2] => another_word
            [3] => do-rae-mi-fa_so_la
        )

)

【讨论】:

  • 哇,太棒了!非常感谢您的帮助:)
  • @user3143218 谢谢!我只是让它变得更好!
  • 您可以从字符类、非捕获组和s修饰符中删除_
  • @CasimiretHippolyte 我添加了_ 目的是为了展示可以使用字母字符的不同之处。但只是在没有_ 的情况下尝试过,这行得通吗?为什么?如果我删除-,测试会失败,只有一个半字?
  • 很好,我使用 preg_match 而不是 preg_match_all 并使用 echo $matches{0] 显示 $regex = '/(?
【解决方案2】:

正视背后就是你所寻找的:

(?<=\bkeyword\s)([a-zA-Z-]+)

应该与preg_match 完美搭配。使用 g 修饰符捕获所有匹配项。

Demo

参考问题:How to match the first word after an expression with regex?

【讨论】:

  • “使用 g 修饰符捕获所有匹配项。”你的意思是 i 修饰符用于 case-insensitive,对吧?
  • 那为什么不用/(?&lt;=\bkeyword\s)([A-Z-]+)/is呢?或/(?&lt;=\bkeyword\s)(?:[A-Z-]+)/is 以免重复从捕获组返回的结果?另外,g 是一个 JavaScript 修饰符。在 PHP 中不起作用。
  • @JakeGould 1 : /(?&lt;=\bkeyword\s)([A-Z-]+)/ 只会捕获大写字母,2: is 只会捕获第一个匹配项,3: g 在 pcre php 中工作并捕获在字符串中找到的所有匹配项。 Demo 你的正则表达式证明了这一点。当您查看演示时,请注意左侧风味标题下方,此演示适用于 pcre php。
  • @JakeGould 我仍然赞成您的回答,因为它更详细。并为提问者服务。 :)
  • 感谢指正。请停止接受建设性的批评——这可能是错误的,但被真诚地提出——如此个人化并做出如此“老兄”的反应。好吗?
【解决方案3】:

虽然正则表达式很强大,但对我们大多数人来说也很难调试和记住。

在这种特殊情况下,Get next word after ... match with PHP,这是一个非常常见的字符串操作。

很简单,通过分解数组中的字符串,并搜索索引。这很有用,因为我们可以指定向前或向后的字数。

这匹配第一次出现 + 1 word:

<?php
$string = explode(" ","This is a string, keyword next, some more text. keyword next-word.");
echo $string[array_search("keyword",$string) + 1];
/* OUTPUT next, *

Run it online

通过反转数组,我们可以捕捉到最后一次出现 - 1 word:

<?php
$string = array_reverse(explode(" ","This is a string, keyword next, some more text. keyword next-word."));
echo $string[array_search("keyword",$string) - 1];
/* OUTPUT next-word. */

Run it online

如果我们进行多次搜索,这对性能有好处,但当然字符串的长度必须保持较短(整个字符串在内存中)。

【讨论】:

    猜你喜欢
    • 2013-09-13
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多