用于检查单个并发到preg

编程入门 行业动态 更新时间:2024-10-28 15:19:18
用于检查单个并发到preg_match_all的模式(Pattern for check single occurrency into preg_match_all)

我正在编写一个函数来检索我传递的所有事件。 我是意大利人所以我认为我可以通过一个例子更清楚。 我会检查一下我的短语是否含有一些水果。

好的,所以让我们看看我的PHP代码:

$pattern='<apple|orange|pear|lemon|Goji berry>i'; $phrase="I will buy an apple to do an applepie!"; preg_match_all($pattern,$phrase,$match);

结果将是一个带有“apple”和“applepie”的数组。

我怎样才能只搜索确切的事件? 阅读我发现的手册: http : //php.net/manual/en/regexp.reference.anchors.php

我尝试使用\A , \Z , ^和$但在我的情况下似乎没有人正常工作!

有人可以帮帮我吗?

编辑:在@ cris85的回答之后我尝试改进我的问题...我的真实模式包含超过200个occorrency并且这个短语超过10000个caracters,所以真实案例太大而无法插入此处。

经过一些试验后,我发现了一个错误的“微软交换”! 我必须逃脱一些特殊的角色吗? 目前我逃避“+”“ - ”“。” “?” “$”和“*”。

I'm writing a function that should retrieve all occurrences that I pass. I'm italian so I think that I could be more clear with an example. I would check if my phrase contains some fruits.

Ok, so lets see my php code:

$pattern='<apple|orange|pear|lemon|Goji berry>i'; $phrase="I will buy an apple to do an applepie!"; preg_match_all($pattern,$phrase,$match);

the result will be an array with "apple" and "applepie".

How can I search only exact occurency? Reading the manual I found: http://php.net/manual/en/regexp.reference.anchors.php

I try to use \A , \Z , ^ and $ but no one seems to work correctly in my case!

Someone can help me?

EDIT: After the @cris85 's answer I try to improve my question ... My really pattern contains over 200 occorrency and the phrase is over 10000 caracters so the real case is too large to insert here.

After some trials I found an error on the occurrency "microsoft exchange"! There is some special caracters that I must escape? At the moment I escape "+" "-" "." "?" "$" and "*".

最满意答案

您尝试使用的锚是完整字符串,而不是每个字。 您可以使用word boundaries来匹配单个单词。 这应该允许您只找到完整的水果匹配:

$pattern='<\b(?:apple|orange|pear|lemon|Goji berry)\b>i';

?:是这样你不会创建一个额外的捕获组,它是一个non-capture group 。

以下是正则表达式对边界匹配的定义:

在字符串中的第一个字符之前,如果第一个字符是单词字符。 在字符串中的最后一个字符之后,如果最后一个字符是单词字符。 在字符串中的两个字符之间,其中一个是单词字符,另一个不是单词字符。

PHP演示: https : //3v4l.org/h5GCf

正则表达式演示: https : //regex101.com/r/5aBaMO/1/

The anchors you tried to use are for the full string, not per word. You can use word boundaries to match individual words. This should allow you to find only complete fruit matches:

$pattern='<\b(?:apple|orange|pear|lemon|Goji berry)\b>i';

The ?: is so you don't make an additional capture group, it is a non-capture group.

Here's the definitation from regex-expressions for what a boundary matches:

Before the first character in the string, if the first character is a word character. After the last character in the string, if the last character is a word character. Between two characters in the string, where one is a word character and the other is not a word character.

PHP Demo: https://3v4l.org/h5GCf

Regex Demo: https://regex101.com/r/5aBaMO/1/

更多推荐

本文发布于:2023-08-04 04:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409768.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:preg

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!