简体   繁体   中英

MySql regexp match all characters

I am matching a regexp in MySQL as follows:

$this->db->where('bottom_id REGEXP', $bottom);

The values of $bottom comes from a array:

$bottom_key = array('EG','LE','KG','IS|BO|COA|ARG|PAW|HSE','JG|KH|BR|LL|MO|RA', 'JGS|KHS|BRS|LLS|KHC|BRC|TY|MOS|RAS', 'LEO|EGS');

When I run the query 'JG|KH|BR|LL|MO|RA' and 'JGS|KHS|BRS|LLS|KHC|BRC|TY|MOS|RAS' duplicate some of the results ie JGS is selected from the JG value.

How do I force an exact match of the values so that JG will only match JG and not JGS?

The data table looks as follows:

|------
|id|order_id|product_id|upper_id|bottom_id|create_date|del_date|ref_no|comm_code
|------
|81|17|6151|HSA       |IS        |0000-00-00 00:00:00|2016-10-15|NULL|NULL
|82|17|7441|BHE       |IS        |0000-00-00 00:00:00|2016-09-15|NULL|NULL
|83|17|7501|MUA       |IS        |0000-00-00 00:00:00|2016-07-15|NULL|NULL
|84|17|7137|KDB       |IS        |0000-00-00 00:00:00|2016-07-07|NULL|NULL
|85|17|7137|KDD       |IS        |0000-00-00 00:00:00|2016-08-15|NULL|NULL
|86|17|6613|PAA       |KG        |0000-00-00 00:00:00|2016-10-15|NULL|NULL
|87|17|7315|CDA       |KG        |0000-00-00 00:00:00|2016-07-07|NULL|NULL
|88|17|7525|COA       |KG        |0000-00-00 00:00:00|2016-07-07|NULL|NULL
|89|17|7525|COA       |KG        |0000-00-00 00:00:00|2016-09-15|NULL|NULL
|90|17|1600|JGC       |MOS       |0000-00-00 00:00:00|2016-08-15|PD65929|NULL

Use

$bottom_key = array('^EG$','^LE$','^KG$','^(IS|BO|COA|ARG|PAW|HSE)$','^(JG|KH|BR|LL|MO|RA)$', '^(JGS|KHS|BRS|LLS|KHC|BRC|TY|MOS|RAS)$', '^(LEO|EGS)$');

The regexp symbol ^ indicates the starting of the string and $ its end.

您可以通过将$放在JG(即JG $)之后来进行严格匹配,这意味着匹配的字符串应以G结尾。这应该可以解决您的问题。

您最好使用一个IN子句,而不是一个正则表达式数组。

bottom_id IN ('EG','LE','KG','IS','BO','COA','ARG','PAW','HSE','JG',...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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