SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with REGEXP

    Say I have a list of 3 key words: 'mi go home'.
    Then user enters into a search area such as
    'm go home'
    that I will assemble them into a regexp such as
    [(m)(go)(home)]

    I would like to REGEXP in mysql to see if user enter any word matching keyword list by select
    ... searchwordField REGEXP '[(m)(go)(home)]';

    'go' and 'home' match, but 'm' should not match 'mi', but it did.

    How to use REGEXP correctly?
    Thanks in advance,
    John
    Last edited by johnn; Mar 15, 2002 at 12:56.

  2. #2
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

    I have tried it with the preg_* functions of PHP and found a regexp that will match what you are looking for.

    I hope it works for mysql aswell:

    PHP Code:
    $text "m go home";
    preg_match("/^m go home$/"$text$result); 
    The output will be:

    Code:
    Array
    (
        [0] => m go home
    )
    It will not match if $text is for example "me go home".

    I don't know the meaning of "[" and "]" in MySQL regexp but normally you define a character range with them.
    I have tested the regexp you used and it will match
    mo goh home as well, since the letters "o" and "h" are used in the word home.

    Hope it helps.

    --Chris

  3. #3
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I think I misunderstood your post.

    Maybe you can split the phrase the user enters into several REGEXP's

    Code:
    searchwordField REGEXP '^m{1} ' 
    AND searchwordField REGEXP 'go '
    AND searchwordField REGEXP 'home'
    '^m{1} ' will match exactly one m at the beginning.

  4. #4
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, Chris. I'm not sure how John Coggeshall can use regexp successfully in the article "Simple Search Engine":
    http://www.zend.com/zend/spotlight/w...engine2may.php

    By the way I used this instead in my search engine code:
    "SELECT ... WHERE ... IN ($word1,$word2,$word3)"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •