English 中文(简体)
MySQL string match with multiple words
原标题:

This page

has a great example using REGEXP to do pattern matching. the problem with REGEXP won t match the following strings:

  • "Mr John"
  • "Dr. John"
    or even:
  • "Mr. John Doe"

with the string "John Doe"

I would like to know how do I get positive matches for any of the given examples?

Here is a sample code:


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 ;

-Thank you

UPDATE: Thank you for the answers, the question is not how to use regular expression to do the matching that I want, but rather how can I do it regardless of REGEXP. I use REGEXP as an example of pattern matching. I do appreciate the clarification on regular expressions.

最佳回答

Regular expressions are not meant to match inexact strings (for example, a spelling error). It seems like that is what you are trying to do. A regular expression could be used, for example, to match any social security number (three digits followed by a hyphen followed by two digits followed by another hyphen followed by four digits). But you couldn t use a regular expression to match misspellings of John. Misspellings are handled using some fancier coding, usually called n-gram matching (see: http://en.wikipedia.org/wiki/N-gram). If you are also using Ruby-on-Rails, there is a great Gem (called Chrononaut-no_fuzz) to handle this for you, but with plain MySQL you may have to hand-code this feature.

问题回答

the string John Doe should match is the last one. Can you please post the exact sql and the data it s trying to match

Ok, so you have the string and pattern mixed up you supply a pattern to regexp intending to match a string. So for example Dr. John will never match John since the pattern tries to match Dr and fails. However John will match Dr John since the pattern now find John within Dr John. My suggestion to you is to read a regular expressions primer.





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签