[英]How to match hyphen delimited in any order
我需要匹配由連字符分隔的一組字符-例如:
B-B/w-W/Br-W-Br
/是我需要的部分,最多20個空格。
G-R-B, G/R-B-B/W-O
因此,我需要一個正則表達式來覆蓋-之間的任何順序(GRB也可以是RBG),而我一直在使用一堆組合命令,但是我無法提出可以匹配任何順序的東西。
計划是使用mysql搜索這種方式。 因此,它將像
select * from table1 where pinout REGEXP '';
我只是不能正確使用正則表達式:/
如果字符串中包含每個連字符分隔的值,則此表達式將與字符串匹配。 顏色值可以按任何順序出現在字符串中,因此此表達式將匹配W/Br-bB/w
和B/wW/Br-b
...或包含這些顏色的任何其他組合。
^ # match the start to of the string
(?=.*?(?:^|-)W\/Br(?=-|$)) # require the string to have a w/br
(?=.*?(?:^|-)b(?=-|$)) # require the string to have a b
(?=.*?(?:^|-)B\/w(?=-|$)) # require the string to have a b/w
.* # match the entire string
MySql並不真正支持環顧四周,因此需要將其分成一組where語句
mysql> SELECT * FROM dog WHERE ( color REGEXP '.*(^|-)W\/Br(-|$)' and color REGEXP '.*(^|-)b(-|$)' and color REGEXP '.*(^|-)B\/w(-|$)' );
+-------+--------+---------+------+------------+---------------------+
| name | owner | species | sex | birth | color |
+-------+--------+---------+------+------------+---------------------+
| Claws | Gwen | cat | m | 1994-03-17 | B-B/w-W/Br-W-Br |
| Buffy | Harold | dog | f | 1989-05-13 | G-R-B, G/R-B-B/W-O |
+-------+--------+---------+------+------------+---------------------+
另請參見此有效的sqlfiddle: http ://sqlfiddle.com/#!2/943af/1/0
將正則表達式與MySql結合使用,可以在此處找到語句: http : //dev.mysql.com/doc/refman/5.1/en/pattern-matching.html
我可能對您的示例有誤解,請嘗試以下操作:
-*([a-zA-Z/]+)-*
捕獲區域可以更改為包括您的特定感興趣的字母,例如[GRBWOgrbwo /]。
編輯:我認為這不會在您使用它的上下文中為您提供幫助,但是我將其留在此處供后代參考。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.