其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

正则表达式获取仅包含模式列表中的单词的字符串?

GG网络技术分享 2025-03-18 16:15 7


问题描述:

Consider the following array elements

 1.benclinton

2.clintonharry

3.harryben

4.benwill

5.jasonsmith

6.smithclinton

Assume the pattern list is ben,harry,clinton, then the result I should get is

1.benclinton  

2.clintonharry

3.harryben

So,essentially the result should contain strings that contains only the words that are in the pattern list. Order is not important

Also, each strings will not be having more than two words. i.e. bensmithwill will never be a case.

Since all my strings are in an array, I thought of using preg_grep in php to do this but i am struck in framing correct regex for this.

what regex can achieve this? Is there any other efficient way apart from regex matching that will do the work?

Thanks in advance!

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

考虑以下数组元素</ p>

  1.benclinton 

2.clintonharry

3.harryben

4.benwill

5.jasonsmith

6.smithclinton

</ code> </ pre>

假设模式列表为 ben, 哈里,克林顿</ strong>,然后我得到的结果是</ p>

  1.benclinton 

2.clintonharry

3.harryben

</ code> </ pre>

因此,基本上结果应该包含仅包含模式列表中的单词的字符串。 顺序并不重要</ p>

此外,每个字符串不会超过两个单词。 即bensmith将永远不会是一个案例。</ p>

由于我的所有字符串都在一个数组中,我想在php中使用preg_grep来做到这一点,但我为此构建了正确的正则表达式。 </ p>

正则表达式可以实现这一目标? 除了可以完成工作的正则表达式匹配之外还有其他有效方法吗?</ p>

提前致谢!</ p>

</ div>

网友观点:

Something like this

$names_list = [\'benclinton\',\'clintonharry\',\'harryben\',\'benwill\',\'jasonsmith\',\'smithclinton\'];

$names = [\'ben\',\'harry\',\'clinton\'];

$matches = preg_grep(\'/(\'.implode(\'|\',$names).\')(?1)/\', $names_list);

//- /(ben|harry|clinton)(?1)/ -- (?1) = recurse capture group 1

print_r($matches);

Output

Array

(

[0] => benclinton

[1] => clintonharry

[2] => harryben

)

Sandbox

This requires that at least two of the names (even the same one 2x) match. But that is kind of a given in this case or everything would match.

If you want to be extra careful, if the $names can contain something important to regex, such as +,*, \\ etc. you can add this

$matches = preg_grep(\'/(\'.implode(\'|\',array_map(function($name){return preg_quote($name,\'/\');},$names)).\')(?1)/\', $names_list);

It appears that you want to match array elements which are exact combinations of two keywords. For a regex approach, we can try taking the cross product of the vector of keyords, and then generate an alternation. Then, we can use preg_grep against your input array to find all matching elements.

$array = array(\\\"benclinton\\\", \\\"clintonharry\\\", \\\"harryben\\\", \\\"benwill\\\", \\\"jasonsmith\\\", \\\"smithclinton\\\");

$input = array(\\\"ben\\\", \\\"harry\\\", \\\"clinton\\\");

$regex = \\\"\\\";

foreach ($input as $term1) {

foreach ($input as $term2) {

if ($regex != \\\"\\\") $regex .= \\\"|\\\";

$regex .= $term1.$term2;

}

}

$regex = \\\"/^(\\\" . $regex . \\\")$/\\\";

$matches = preg_grep($regex, $array);

print_r($matches);

Array

(

[0] => benclinton

[1] => clintonharry

[2] => harryben

)

Here is the regex alternation generated by the above script:

(benben|benharry|benclinton|harryben|harryharry|harryclinton|clintonben|

clintonharry|clintonclinton)

Without Regex.Do with array_filter and strpos

  1. Filter array with respected matching second array the count greater then 1

Sandbox

<?php

$a = [\'benclinton\',\'clintonharry\',\'harryben\',\'benwill\',\'jasonsmith\',\'smithclinton\'];

$a2 = [\'ben\',\'clinton\',\'harry\'];

$res = array_filter($a,function($str=\\\"\\\") use($a2){

$r =array_filter($a2,function($a2str) use($str){

return strpos($str,$a2str) !== FALSE;

});

return count($r) > 1;

});

print_r($res);

?>

正则表达式

正则表达式简介

正则表达式是字符串处理的利器,它可以用于字符串的匹配、查找、替换和劈分。借助正则表达式、标准库模块 re ,实现。

正则表达式是一个特殊的字符串,该字符串中包含了一些特定字符,这些特定字符都有明确的匹配规则,因此,正则表达式是一个定义了匹配规则字符串。如果某个字符串符合正则表达式定义的所有匹配规则,那么该字符串就与正则表达式匹配。

正则表达式中,普通字符用于精确匹配,特定字符具有明确的匹配规则。

正则表达式的语法

1 . 单个字符匹配
2 . 贪婪匹配和勉强匹配
3 . 分组匹配
4 . 逻辑匹配
5 . 边界匹配

re 模块的使用

如果想判断给定的字符串和正则表达式是否匹配,可以使用模块 re 提供的方法:
match ( pattern, string [, flags] )
该方法会根据参数 string 指定的字符串与 参数 pattern 指定的正则表达式进行匹配 。
pattern 是一个正则表达式,或对正则表达式预编译之后得到的对象
flags 是一个标志位,用于控制正则表达式匹配方式,如:是否区分大小写、多行匹配等。

match 方法匹配规则

从参数 string 指定的字符串开头开始,一直向后尝试匹配参数 pattern 指定的正则表达式,在到达正则表达式末尾之前,如果遇到了无法匹配的字符 或 到达了字符串的末尾 都表示匹配失败,返回 None。

否则,当到达 pattern 的末尾时,如果所有字符都是匹配成功的,则表示匹配成功,从而终止匹配,不再对 string 向后匹配,同时返回一个 Match 对象。

r'...' 表示原始字符串,无需考虑转义字符问题,建议这么写。

标签:

提交需求或反馈

Demand feedback