Wildcard search function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • google@grepit.com

    Wildcard search function

    I need a simple wildcard pattern matching function written in JS. I
    have wrestled with regular expresions but frankly am struggling to come
    up with anything less than an epic function of many lines of code ...
    Any help much appreciated!

    Sample string :
    "the cat sat on the mat"

    matches :
    "the cat*" , "*cat*" , "*the mat"

    no matches :

    "the cat", "cat", "the mat"

  • Evertjan.

    #2
    Re: Wildcard search function

    wrote on 23 okt 2006 in comp.lang.javas cript:
    I need a simple wildcard pattern matching function written in JS. I
    have wrestled with regular expresions but frankly am struggling to come
    up with anything less than an epic function of many lines of code ...
    Any help much appreciated!
    >
    Sample string :
    "the cat sat on the mat"
    >
    matches :
    "the cat*" , "*cat*" , "*the mat"
    >
    no matches :
    >
    "the cat", "cat", "the mat"
    I cannot imagine you "need" it in javascript,
    perhaps you only "want" it, or it is a school assignment.

    When seaching a large amount of data, say on a database,
    SQL string with "LIKE" will help you.

    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)

    Comment

    • google@grepit.com

      #3
      Re: Wildcard search function

      It is needed and your reply is not at all helpful

      Comment

      • Julian Turner

        #4
        Re: Wildcard search function


        google@grepit.c om wrote:
        I need a simple wildcard pattern matching function written in JS. I
        have wrestled with regular expresions but frankly am struggling to come
        up with anything less than an epic function of many lines of code ...
        Any help much appreciated!
        >
        Sample string :
        "the cat sat on the mat"
        >
        matches :
        "the cat*" , "*cat*" , "*the mat"
        >
        no matches :
        >
        "the cat", "cat", "the mat"
        Hi

        Here is a starter for ten, covering "the cat*", and not using any
        regular expressions.

        I assume that "the cat*" means that the sample string **must** start
        with "the cat". Your post is perhaps ambiguous in this regard.

        Perhaps you could have a go extending to the other matches you want,
        case handling, making it more robust, etc, and then post your code.

        var s = "the cat sat on the mat";

        alert (match(s, "the cat"));
        alert (match(s, "the cat*"));

        function match(sSource, sMatch)
        {
        var wildcardRight = false;

        if (sMatch.charAt( sMatch.length - 1) == "*")
        {
        wildcardRight = true;
        sMatch = sMatch.substrin g(0, sMatch.length - 1);
        }

        if (wildcardRight)
        {
        return (sSource.indexO f(sMatch) == 0);
        }
        else
        {
        return sSource == sMatch;
        }
        }

        Regards

        Julian

        Comment

        • Evertjan.

          #5
          Re: Wildcard search function

          wrote on 25 okt 2006 in comp.lang.javas cript:
          It is needed and your reply is not at all helpful
          Are you addressing me perhaps?

          Without quoting I don't know what you are talking about.

          In general things are seldom "needed", often "wanted".

          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress)

          Comment

          • Dr J R Stockton

            #6
            Re: Wildcard search function

            In message <1161614616.104 428.113250@k70g 2000cwa.googleg roups.com>, Mon,
            23 Oct 2006 07:43:36, google@grepit.c om writes
            >I need a simple wildcard pattern matching function written in JS. I
            >have wrestled with regular expresions but frankly am struggling to come
            >up with anything less than an epic function of many lines of code ...
            >Any help much appreciated!
            >
            >Sample string :
            >"the cat sat on the mat"
            >
            >matches :
            >"the cat*" , "*cat*" , "*the mat"
            >
            >no matches :
            >
            >"the cat", "cat", "the mat"
            The string matches none of the substrings, but the substrings match
            parts of the string. Replace in your substrings "*" by ".*", prefix by
            ^, follow by $, and you are there. Simplify : ^.* and .*$ can be
            removed.

            Testing in <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#RT>, your
            conditions are fulfilled.

            Example :
            S = "the cat sat on the mat"
            OK = !!S.match(/^the cat.*$/) // true
            OK = !!S.match(/^the cat$/) // false


            It's a good idea to read the newsgroup and its FAQ. See below.

            --
            (c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
            <URL:http://www.jibbering.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • google@grepit.com

              #7
              Re: Wildcard search function

              If you have nothing constructive to add to this topic please move
              along.

              On Oct 25, 3:09 pm, "Evertjan." <exjxw.hannivo. ..@interxnl.net wrote:
              wrote on 25 okt 2006 in comp.lang.javas cript:
              >
              It is needed and your reply is not at all helpfulAre you addressing me perhaps?
              >
              Without quoting I don't know what you are talking about.
              >
              In general things are seldom "needed", often "wanted".
              >
              --
              Evertjan.
              The Netherlands.
              (Please change the x'es to dots in my emailaddress)

              Comment

              • google@grepit.com

                #8
                Re: Wildcard search function

                Thanks for this.

                I have two routines now as shown below, both seem to work fine, but the
                performance of the regular expression approach is not good - any ideas?
                (String1 = string to be searched & String2 contains the pattern)

                function MatchString(Str ing1, String2)
                {
                var Items, Pos;

                Items = String2.split(" *");
                if (Items.length == 1)
                return(String1 == String2);
                Pos = 0;
                for (Count=0; Count<Items.len gth; Count++)
                {
                if (Count == 0)
                {
                if ((Pos = String1.indexOf (Items[Count], Pos)) != 0)
                return(false);
                }
                else
                {
                if ((Pos = String1.indexOf (Items[Count], Pos)) == -1)
                return(false);
                }
                Pos = Pos + Items[Count].length;
                }
                return(true);
                }

                function MatchString2(St ring1, String2)
                {
                var Pattern = "";

                Pattern = '/^' + String2 + '$/';
                return(!!String 1.match(eval(Pa ttern)));
                }

                On Oct 25, 9:29 pm, Dr J R Stockton <j...@merlyn.de mon.co.ukwrote:
                In message <1161614616.104 428.113...@k70g 2000cwa.googleg roups.com>, Mon,
                23 Oct 2006 07:43:36, goo...@grepit.c om writes
                >
                I need a simple wildcard pattern matching function written in JS. I
                have wrestled with regular expresions but frankly am struggling to come
                up with anything less than an epic function of many lines of code ...
                Any help much appreciated!
                >
                Sample string :
                "the cat sat on the mat"
                >
                matches :
                "the cat*" , "*cat*" , "*the mat"
                >
                no matches :
                >
                "the cat", "cat", "the mat"The string matches none of the substrings, but the substrings match
                parts of the string. Replace in your substrings "*" by ".*", prefix by
                ^, follow by $, and you are there. Simplify : ^.* and .*$ can be
                removed.
                >
                Testing in <URL:http://www.merlyn.demo n.co.uk/js-valid.htm#RT>, your
                conditions are fulfilled.
                >
                Example :
                S = "the cat sat on the mat"
                OK = !!S.match(/^the cat.*$/) // true
                OK = !!S.match(/^the cat$/) // false
                >
                It's a good idea to read the newsgroup and its FAQ. See below.
                >
                --
                (c) John Stockton, Surrey, UK. ?...@merlyn.dem on.co.uk Turnpike v6.05 IE 6
                <URL:http://www.jibbering.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                • Evertjan.

                  #9
                  Re: Wildcard search function

                  wrote on 31 okt 2006 in comp.lang.javas cript:
                  On Oct 25, 3:09 pm, "Evertjan." <exjxw.hannivo. ..@interxnl.net wrote:
                  > wrote on 25 okt 2006 in comp.lang.javas cript:
                  >>
                  It is needed and your reply is not at all helpfulAre you addressing
                  me perhaps?
                  >>
                  >Without quoting I don't know what you are talking about.
                  >>
                  >In general things are seldom "needed", often "wanted".
                  [Please do not toppost on usenet]
                  If you have nothing constructive to add to this topic please move
                  along.
                  How can I add anything if you do not, by quoting,
                  tell us what you were talking about first?

                  Please keep to netiquette!

                  --
                  Evertjan.
                  The Netherlands.
                  (Please change the x'es to dots in my emailaddress)

                  Comment

                  • Julian Turner

                    #10
                    Re: Wildcard search function


                    google@grepit.c om wrote:

                    [snip]
                    but the
                    performance of the regular expression approach is not good - any ideas?
                    [snip]
                    function MatchString2(St ring1, String2)
                    {
                    var Pattern = "";
                    >
                    Pattern = '/^' + String2 + '$/';
                    return(!!String 1.match(eval(Pa ttern)));
                    }
                    Hi

                    Your use of "eval" may be one source of the slow performance. You are
                    generally wise to avoid using eval unless absolutely necessary.

                    new RegExp() will parse a String into a RegExp

                    Try

                    var re = new RegExp(Pattern) ;
                    return !!re.test(Strin g1);

                    Regards

                    Julian

                    Comment

                    • Julian Turner

                      #11
                      Re: Wildcard search function


                      google@grepit.c om wrote:

                      [snip]
                      but the
                      performance of the regular expression approach is not good - any ideas?
                      [snip]
                      function MatchString2(St ring1, String2)
                      {
                      var Pattern = "";
                      >
                      Pattern = '/^' + String2 + '$/';
                      return(!!String 1.match(eval(Pa ttern)));
                      }
                      Hi

                      Apologies, there was an error in my last post, in that for new RegExp,
                      you ignore the "/" at the start and end of a RegExp literal.

                      Furthermore, if by performance you mean that it is not returning the
                      result you expect, then it may depend on the following.

                      If String2 value is "the cat*", the ' * ' does not have quite the same
                      meaning as a wild-card in a regular expression.

                      ' * ' in a regular expression looks for the previous character repeated
                      ad-infinitum. Notice the dot ".*" in the examples you have been given.
                      ' . ' means any caracter, other than new line I think.

                      An alternative approach may be to do the following:-

                      function match(s1, s2)
                      {
                      var left = (s2.charAt(0) == "*") ? "" : "^";
                      var right = (s2.charAt(s2.l ength - 1) == "*") ? "" : "$";
                      var s2 = s2.replace(/^\*|\*$/,"");
                      var re = new RegExp(left + s2 + right);
                      return re.test(s1);
                      }


                      One thing to watch out for. Certain characters, like "(" have a
                      special meaning in a RegExp, so they must either not be included in s2,
                      or be escaped : i.e. "(" -"\(".

                      Regards

                      Julian Turner

                      Comment

                      • google@grepit.com

                        #12
                        Re: Wildcard search function

                        What would you know about "netiquette " as you so naffly put it.

                        You have posted three times to this thread and have come up with
                        nothing constructive at all. You kindly suggested that I did not "need"
                        a solution with your first post and perhaps it was my "homework" -
                        which was completely useless and confontational.

                        I suggest that you post only to threads where you actually have
                        something constructive and useful to say, something akin to the very
                        informative suggestions from the other participants.

                        Tw@t!

                        Evertjan. wrote:
                        wrote on 31 okt 2006 in comp.lang.javas cript:
                        >
                        On Oct 25, 3:09 pm, "Evertjan." <exjxw.hannivo. ..@interxnl.net wrote:
                        wrote on 25 okt 2006 in comp.lang.javas cript:
                        >
                        It is needed and your reply is not at all helpfulAre you addressing
                        me perhaps?
                        >
                        Without quoting I don't know what you are talking about.
                        >
                        In general things are seldom "needed", often "wanted".
                        >
                        [Please do not toppost on usenet]
                        >
                        If you have nothing constructive to add to this topic please move
                        along.
                        >
                        How can I add anything if you do not, by quoting,
                        tell us what you were talking about first?
                        >
                        Please keep to netiquette!
                        >
                        --
                        Evertjan.
                        The Netherlands.
                        (Please change the x'es to dots in my emailaddress)

                        Comment

                        Working...