Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Match-order

Last post 05-04-2007, 6:56 AM by eSquire. 3 replies.
Sort Posts: Previous Next
  •  05-03-2007, 8:18 AM 29443

    Match-order

    Hello,

     

    I wonder if someone could help me out here: I have a string holding a value "abcdefghijk stuvwxyz". If I search with regex

     

    "(.*abc.*)+(.*uvw.*)+";

     

    I get a match, no problem. But if I search for

     

    "(.*uvw.*)+(.*abc.*)+"; 

     

    I get no match. is there a way where you could ignore the order of the words in some way? I would like to be able to search within a text for one or several words or part of words, but all words that are entered in the search-field MUST appear in the text to be searched through - no matter what order.

     

    thanks!

     

    Christoph 

     

     

  •  05-03-2007, 11:56 AM 29450 in reply to 29443

    Re: Match-order

    You may achieve what you want using lookahead (provided your regex dialect understands it):

    ^(?=.*abc)(?=.*xyz) 
    will match if both words are found. You won't have capturing groups with the found words though.
    Filed under:
  •  05-04-2007, 1:35 AM 29461 in reply to 29450

    Re: Match-order

    Hello there,

     

    I tried this out, but get a warning in PHP, saying that this is an invalid call of my MySQL-query (saying that I use the RegExp-function in MySQL). Is there any other way to solve this?

     

    Christoph 

  •  05-04-2007, 6:56 AM 29465 in reply to 29461

    Re: Match-order

    Looks like MySQL does not support lookarounds then.

    You could try to put all possible permutations into your pattern, but this gets ugly soon:

    abc.*uvw|uvw.*abc

    May I ask why you don't stick with SQL?

    SELECT * FROM table t WHERE  t.column LIKE '%abc%' AND t.column LIKE '%uvw%';

    doesn't even need a regular expression and might be quite a bit faster.

    If you absolutely have to use a regex, try:

    SELECT * FROM table t WHERE  t.column REGEXP 'abc' AND t.column REGEXP 'uvw';
View as RSS news feed in XML