Results 1 to 7 of 7

Thread: [RESOLVED] Possible to search for two words with RegExp

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Resolved [RESOLVED] Possible to search for two words with RegExp

    This is not my code - but appears to be how a jQuery autocomplete can fill the array that shows when you start typing.

    Code:
    var matcher = new RegExp($.ui.autocomplete.escapeRegex(wesInput.val().toLowerCase()), "i");
    var arraytoreturn = $.grep(wesInput.autocomplete("option", "source"), function(value) {
        return matcher.test(value.label || value.value || value);
    });
    So - if I have

    SMITH, ANN
    SMITH, JOHN
    SMITH, JOHN AND AMY
    SMITH, JOSEPH
    SMITH, JOSEPH AND ANN

    And I type SMITH, JOS

    I currently get

    SMITH, JOSEPH
    SMITH, JOSEPH AND ANN

    So I guess that means that the "standard no regexp escape sequences" means "find the data with left-most matching" - GENERIC as we called it back in the mainframe days.

    What could I type that would tell the MATCHER.TEST function to return these two records (if it was a simple world it would be something like SMITH*ANN)

    SMITH, ANN
    SMITH, JOSEPH AND ANN

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Possible to search for two words with RegExp

    Really? A whole week goes by and no one has an any ideas?

    Do I have to split up the two words myself and code some kind of two pass search?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3
    Addicted Member Pc Monk's Avatar
    Join Date
    Feb 2010
    Posts
    188

    Re: Possible to search for two words with RegExp

    no idea means two thing : its impossible OR who knows
    and no you cant do that as far as i know , what do you mean by two pass search anyway ? as you said this piece of code will do find the data with left-most matching and thats all but you can say when you reach to "," stop and use another box for last name search ( guess that means two pass search )
    Body Language tells the truth! even from the grave tsaeb eht morf gninnur ,nwod deaH
    All the big things started from little! teef my tsap evom sekans ,duol raor slluB
    Lietome.ir

  4. #4

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Possible to search for two words with RegExp

    likeIf I was doing it in SQL it would be something like:

    Where PersonName like 'SMITH%ANN%'

    I assumed REGEXP could easily do something similar...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Possible to search for two words with RegExp

    Actually I just discovered that this does work

    (?=.*smith)(?=.*ann)

    This line of code

    var matcher = new RegExp($.ui.autocomplete.escapeRegex(term), "i");

    was messing up the process by ESCAPING the ('s and )'s and such.

    I started with typing (SMITH|ANN) - which got escaped into \(SMITH\|ANN\) - I guess so you could literally search for those REGEX-type characters.

    I changed it to var matcher = new RegExp(term, "i"); and now I can type real REGEX type search factors (if I actually wanted to).

    Of course having the user type (?=.*smith)(?=.*ann) is way too complicated - but I can have them enter SMITH*ANN and I can detect the "*" character and I can change that into a real REGEX search factor.
    Last edited by szlamany; May 29th, 2013 at 03:37 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Possible to search for two words with RegExp

    So - I ended up writing my own makeRegex function.

    Here's the final code

    Code:
                $.extend($.ui.autocomplete, {
                    escapeRegex: function(value) {
                        return value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
                    },
                    filter: function(array, term) {
                        var matcher = makeRegex(term); //new RegExp($.ui.autocomplete.escapeRegex(term), "i");
                        var arraytoreturn = $.grep(array, function(value) {
                            return matcher.test(value.label || value.value || value);
                        });
                        if (arraytoreturn.length > 200) {
                            arraytoreturn = [{ label: "Please be more specific - " + arraytoreturn.length + " matching entries!", value: ""}];
                        }
                        return arraytoreturn;
                    }
                });
    You can see the escapeRegex function that was in place.

    The makeRegex function looks like this

    Code:
            function makeRegex(term) {
                var arrTerms = term.split(" ");
                var strTerms = "";
                if (arrTerms.length > 1) {
                    for (var i = 0; i < arrTerms.length; i++) {
                        arrTerms[i] = "(?=.*" + arrTerms[i] + ")";
                    }
                    strTerms = arrTerms.join("");
                } else {
                    strTerms = $.ui.autocomplete.escapeRegex(term);
                }
                var regexMatcher = new RegExp(strTerms, "i");
                return regexMatcher;
            }

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: [RESOLVED] Possible to search for two words with RegExp

    It might be too late now, but I was going to suggest looking at http://xregexp.com/ to see if that had anything useful.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

Posting Permissions

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



Click Here to Expand Forum to Full Width