PHP preg_match(_all)用于文本范围

问题描述 投票:1回答:1

试图获取Textrange(前后n个单词)搜索字符串(我自己)

$text = 'Me, my dog and “myself“ are going on a vacation. Irene and myself are broke. Myself is here :P John and myself!';

 preg_match_all("/(?:[^ ]+ ){0,2}(?:[“'"(‘. ])myself(?:[“'")‘. ])(?: [^ ]+){0,2}/", $text, $matches);   

这给了我匹配:

•狗和“我自己”正在前进

•我自己

但它应该是:

•狗和“我自己”正在前进

•艾琳和我自己都破产了

•约翰和我自己!

请帮助我找到所有匹配的文本范围2个字之前和2个字之后。无论在搜索字符串(我自己)或“我自己”或“我自己”之前或之后是否有特殊的字符或空格...

thanks.Sepp

php regex preg-match
1个回答
1
投票

问题出现的原因是[“'"(‘. ][“'")‘. ]都是强制性的,需要在myself之前和之后有一个字符。然后,在myself(?:[^ ]+ ){0,2}要求的(?: [^ ]+){0,2}之前和之后还必须有另一个空间。

你可以用

'/(?:\S+\s+){0,2}(?:[“'"(‘.])?myself(?:[“'")‘.]?)(?:\s+\S+){0,2}/u'

或者允许myself周围的任何标点符号与\p{P}

'/(?:\S+\s+){0,2}\p{P}?myself\p{P}?(?:\s+\S+){0,2}/u'

regex demo

请注意,(?:[“'"(‘.])?(?:[“'")‘.]?)(或\p{P}?)都是可选的,它们之后的?量词使得正则表达式引擎仅匹配这些模式的1或0次出现。因此,如果它存在或不存在,则匹配发生。

PHP demo

$text = 'Me, my dog and “myself“ are going on a vacation. Irene and myself are broke. Myself is here :P John and myself!';
if (preg_match_all('/(?:\S+\s+){0,2}\p{P}?myself\p{P}?(?:\s+\S+){0,2}/u', $text, $result)) {
    print_r($result[0]);
}

输出:

Array
(
    [0] => dog and “myself“ are going
    [1] => Irene and myself are broke.
    [2] => John and myself!
)
© www.soinside.com 2019 - 2024. All rights reserved.