Match sequence of characters in regular expression
我想匹配字符串中的一系列字母。例如,如果我有字母 T B E 我想匹配所有以字母 T 开头并包含字母 B 和 E 至少一次的字符串。第二个字母必须出现在第三个之前,并且字母之间可能有无数个字符。
那是字母 T B E 将匹配字符串
表,麻烦和可怕,但不是 Teb。
我正在尝试使用
在 php 中对此进行编码
1 2 3 4 5
|
$A = ‘T’;
$B = ‘B’;
$C = ‘E’;
$matches = preg_grep(‘/^’.$A.‘.+’.$B.‘.+’.$C.‘/’, $words);
|
其中 words 是一个包含单词列表的数组。以我的算法工作,但我无法找到 $A $B 或 $C 之间没有字母的单词。
我将如何使用正则表达式来解决这个问题?
- Terrible 不应该匹配。 e 出现在 b 之前
-
好吧,我希望 Terrible 在这种情况下匹配。重要的是 e 在 b 之后至少出现一次。之前发生与否都没有关系。
您无法找到 $A $B 或 $C 之间没有字母的单词的原因是因为您正在使用 .+ 试图匹配 $A $B 或 $C 之间的至少 1 个字符.
使用 .* 代替 .+
- 您还应该使正则表达式不区分大小写以匹配 Table (/^T[^E]*B.*E$/i) 等单词
来源:https://www.codenong.com/14664277/
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_10036.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57