简体   繁体   English

正则表达式:匹配包含序列的单词

[英]Regular expressions: matching words containing sequences

I am trying to match words containing the following: eph gro iss 我试图匹配包含以下内容的单词: eph gro iss

I have eph|gro|iss which will match eph gro iss in this example: new grow miss eph . 在这个例子中,我有eph|gro|iss匹配eph gro issnew grow miss eph However I need to match the whole word. 但是我需要匹配整个单词。 For example it should match all of the miss not just iss and grow not just gro Thanks 例如,它应该匹配所有的miss不只是issgrow不仅仅是gro谢谢

You can do it like this: 你可以这样做:

\b(\w*(eph|gro|iss)\w*)\b

How it works: 这个怎么运作:

The expression is bracketed with word-boundary anchors \\b , so it only matches whole words. 表达式用词边界锚点\\b括起来,所以它只匹配整个单词。 These words must contain one of the literals eph , gro or iss somewhere, but the \\w* parts allow the literals to appear anywhere within the whole word. 这些单词必须包含其中一个文字ephgroiss ,但是\\w*部分允许文字出现在整个单词中的任何位置。

The important thing here is that you need to adopt some specific definition for "words". 这里重要的是你需要对“单词”采用一些特定的定义。 If you are OK with the regex definition that words are sequences that match [a-zA-Z0-9_]+ then you can use the above verbatim. 如果您对正则表达式定义没有问题,那么单词是与[a-zA-Z0-9_]+匹配的序列,那么您可以逐字使用上述内容。

If your definition of word is something else, you will need to replace the \\b anchors and \\w classes appropriately. 如果您对单词的定义是其他内容,则需要适当地替换\\b anchors和\\w类。

Try this: 尝试这个:

\b([a-zA-Z]*(?:eph|gro|iss)[a-zA-Z]*)\b

Breakdown: 分解:

  • \\b - word boundary \\b - 单词边界
  • ( - start capture ( - 开始捕获
  • [a-zA-Z]* - zero or more letters [a-zA-Z]* - 零个或多个字母
  • (?:eph|gro|iss) - your original regex, non-capturing (?:eph|gro|iss) - 你的原始正则表达式,非捕获
  • [a-zA-Z]* - zero or more letters [a-zA-Z]* - 零个或多个字母
  • ) - end capture ) - 结束捕获
  • \\b - word boundary \\b - 单词边界

Example output: 示例输出:

php > $string = "new grow miss eph";
php > preg_match_all("/\b([a-zA-Z]*(?:eph|gro|iss)[a-zA-Z]*)\b/", $string, $matches);
php > print_r($matches);
Array
(
    [0] => Array
        (
            [0] => grow
            [1] => miss
            [2] => eph
        )

    [1] => Array
        (
            [0] => grow
            [1] => miss
            [2] => eph
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM