【发布时间】:2010-12-15 14:07:26
【问题描述】:
This page
有一个很好的例子,使用 REGEXP 进行模式匹配。 REGEXP 的问题与以下字符串不匹配:
- “约翰先生”
- “约翰博士”
甚至:
- “Mr. John Doe”
使用字符串“John Doe”
我想知道如何获得给定示例的正匹配?
这是一个示例代码:
Drop table Names;
CREATE TABLE Names (
first_name VARCHAR(20),
last_name VARCHAR(20)
);
INSERT INTO Names VALUES ('John','Doe');
INSERT INTO Names VALUES ('Sue','Yin');
INSERT INTO Names VALUES ('Diego James', 'Franco');
select * from Names;
/*To find names containing a string */
/*I want this to march John Doe*/
SELECT * FROM Names WHERE first_name REGEXP 'Mr John';
/*This has John misspelled, I want it to match John Doe */
SELECT * FROM Names WHERE first_name REGEXP 'Hohn' AND last_name REGEXP 'Doe';
/*And this would match Diego James Franco*/
SELECT * FROM Names WHERE first_name REGEXP 'Dr Diego' AND last_name REGEXP 'Franco';
-谢谢
更新: 谢谢你的回答,问题不是如何使用正则表达式来做我想要的匹配,而是不管REGEXP我怎么做。我使用 REGEXP 作为模式匹配的示例。我很欣赏对正则表达式的澄清。
【问题讨论】:
标签: mysql regex string pattern-matching