Announcement

Collapse
No announcement yet.

searching strings & tables for a matching term

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • searching strings & tables for a matching term

    When searching for the term, "lua script docs" in the following string:

    Code:
    http://www.gammon.com.au/scripts/lua.script.docs/strings.htm
    is there a function I can use so that any characters such as periods, hyphens, or underscores are taken into account during the search?

    Or to iterate, if I have a table like this:

    Code:
    http://www.gammon.com.au/scripts/lua.script.docs/strings.htm
    http://www.gammon.com.au/scripts/lua-script-docs/strings.htm
    http://www.gammon.com.au/scripts/lua_script_docs/strings.htm
    and the user enters the search-term, "lua script docs", I'd want to get a positive boolean return on all 3 items in the table.

    I've looked into the string.find, string.match and string.gsub functions but can't seem to find what I'm looking for.

  • #2
    You can use string.match() for this, in two ways...

    You can construct a pattern like "lua.script.docs" and search for that. The dot means any character, so the pattern would match if there is a space, a dot, an underline or any other symbol between these words. However, this pattern requires that these three words appear exactly in this order.

    The other way would be performing three independent searches, one for each substring, and if all three strings are found, you would present the result.

    Ulrich

    Comment


    • #3
      Right, okay. Got that working regarding your first suggestion. Thank you.
      But:

      Is there a way to ensure that pattern-matching doesn't have to be case-sensitive? I notice that if the user enters an upper-case instead of a lower-case letter that the pattern match fails.

      And in a case where the search-term is being pulled from a user's input-box (and is therefore unknown), can you suggest a less clumsy way of going about it than this?

      Code:
      s = "http://www.gammon.com.au/scripts/lua_script_docs/strings.htm"
      
      strSearchTerm = Input.GetText("Input1");
      strLength = String.Length(strSearchTerm);
      str_First_Three_Charaters = String.Left(strSearchTerm, 3);
      nDots = strLength - 3
      nLoopDots = nDots - 1
      
      min = 0; 
      max = nLoopDots; 
      step = 1; 
      for count = min, max, step do
      	TextFile.WriteFromString("C:\\MyFile.txt", ".", true);
      end
      
      patternAppend = TextFile.ReadToString("C:\\MyFile.txt");
      File.Delete("C:\\MyFile.txt", false, false, true, nil);
      
      StrPattern = str_First_Three_Charaters..patternAppend
      
      Debug.ShowWindow(true);
      Debug.Print (string.match (s, StrPattern))

      Comment


      • #4
        Have a look at this:



        You can set three strings to search for, as shown in the image above. The case doesn't matter, because I construct a pattern for both lower case and upper case, so typing "a" or "A" will both result in the same pattern "[aA]", meaning that both of the two options will be searched for.

        Please note that this search assumes that there is a single character between the search terms - if you wish to modify this, replace the dot "." with ".*" (zero or more occurrences) or ".+" (one or more occurrences):



        This should get you started.

        Ulrich
        Attached Files
        Last edited by Ulrich; 06-15-2016, 10:31 PM.

        Comment


        • #5
          Wow yes, that'll get me started for sure. Thank you very much for the fine example, Ulrich. I'm still trying to get a proper handle on much of the code, as it achieves exactly what I'm trying to do.
          So, thanks again. :yes

          I don't suppose you could 'briefly' elaborate on your use of the '#' symbol as an operator in the for do statements (I don't quite understand that part and am having trouble finding any documentation on its use in Lua).

          Comment


          • #6
            "#" is the length operator in Lua. Using # on a string will return the number of chars, or using it on a table will return the number of elements.

            #sFoo could be replaced with String.Length(sFoo), and #tBar could be replaced with Table.Count(tBar). I prefer to type the shorter version.

            Ulrich

            Comment


            • #7
              Originally posted by Ulrich View Post
              "#" is the length operator in Lua. Using # on a string will return the number of chars, or using it on a table will return the number of elements.

              #sFoo could be replaced with String.Length(sFoo), and #tBar could be replaced with Table.Count(tBar). I prefer to type the shorter version.

              Ulrich
              I know I had this explained before but thanks for the refresh there, I thought it was only count on tables so I learnt something here
              Plugins or Sources MokoX
              BunnyHop 2021 Here

              Comment


              • #8
                Okay, I see. Thank you. After studying your code again, that's starting to make some sense to me now. Some of the stuff in the ref-manual over at lua.org can be quite confusing. Now I can begin experimenting a bit more.

                Cheers for your kind help and patience.

                Comment

                Working...
                X