简体   繁体   English

正则表达式以匹配包含(一个或多个)强制和允许字符的整个单词

[英]regex to match entire words containing (one or more) mandatory and allowed characters

I want to match entire words that containing at least one of mandatory chars and allowed chars. 我想匹配包含至少一个必填字符和允许的字符的整个单词。

For example 例如
Mandatory chars are : [ t , a , x ] 强制字符为:[t,a,x]
Allowed chars are : [ i , e] 允许的字符为:[i,e]

t     : passed (one of mandatories are here)
tea   : passed (two of mandatories(t,a) and one allowed(e) here)
e     : failed (none of mandatory is here)
teas  : failed (two of mandatories(t,a) and one allowed(e) here but one intruder(s))

What is the appropriate REGEX code for this? 合适的REGEX代码是什么? It will be used for search 12.000 rows of MySQL table containing one word each row as a PHP project. 它将用于搜索12.000行的MySQL表,其中每行一个单词作为一个PHP项目。

Rather than giving a straight answer, let me help you help yourself. 除了给您一个直接的答案外,让我帮您自助。 A word that passes consists of a sequence of: 通过的单词由以下序列组成:

  • zero or more allowed or mandatory characters 零个或多个允许或必填字符
  • a mandatory character 强制性字符
  • zero or more allowed or mandatory characters 零个或多个允许或必填字符

Write regexes for each of these, then just concatenate them to get a regex for the entire thing. 为每一个都编写正则表达式,然后将它们连接起来就可以得到整个事情的正则表达式。

In perl it would be.. 在perl中会..

$string =~ /^[tax]*[ie]+$/i; #i is for ignore case

* is a 1 or more + is a 0 or more *是1或更大+是0或更大

I just realized you wanted entire words hold on let me rewrite it.. 我只是意识到您想保留所有单词,让我重写它。

the ^ and $ will match start and end of line. ^和$将匹配行的开始和结束。

You can use this pattern: 您可以使用以下模式:

\b[ie]*+[taxie]++\b

explanation: 说明:

since [ie]*+ has a word boundary on the left and a possessive quantifier, it grab all i and e as possible and will never give them back, then the next character must be a t , an a or an x from the next class with the + quantifier that impose at least 1 character. 由于[ie]*+在左侧具有单词边界和所有格量词,因此它会尽可能抓住所有ie ,并且永远不会将其退还,因此下一个字符必须是下一个字符tax带有至少包含1个字符的+量词的类。

The word boundary on the right disallow other kind of characters. 右侧的单词边界禁止使用其他类型的字符。

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

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