Announcement

Collapse
No announcement yet.

Patterns

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

  • Patterns

    Please help me, why the expression (% d% d% d?) it works well for me but it does not do it when it finds 000 (zeros)

  • #2
    What exactly are you trying to run a match on? It's not clear. If you demonstrate what you're attempting by showing a code snippet, a more precise answer can be supplied.

    But, here's a general explanation which should help:
    • %d represents any digit (0-9).
    • %d%d%d represents any 3 digits (0-9)

    Code:
    local s = "000"
    Dialog.Message("", s:match "%d%d%d")

    The ? symbol is a modifier which matches an optional character from another character-class. So for example, if you wanted to append your "000" string with a lower case letter (a-z), and still return a match, then you'd add the ? symbol after the character-class for a-z (%a).

    Code:
    local s = "000h"
    Dialog.Message("", s:match "%d%d%d[%a]?")

    Pattern matching can get quite confusing, so study the character classes and what they mean. Here are some of the best references for studying it:



    Comment


    • #3
      BioHazard you're right, I'll be more specific, I'm doing a directory monitor with TV series. This monitor should inform how many chapters each series has and if one is missing. My chapter names are as follows:

      1x01_24 Hours.avi

      Here 1x is the season number and what follows is the chapter number.

      But it can also be

      4x001_24 Hours.avi

      I need to capture the chapter number, for that I made the following expression:

      ncaptured = String.match (str, "x (% d% d% d?) _")

      Where str is the name of the chapter and works for all cases until it reaches:

      5x000_24 Hours.avi

      And I can not find an explanation

      Comment


      • #4
        1st problem:
        String.match() is not a valid AMS function. So how are you getting it to match any string at all, let alone "5x000_24 Hours.avi"? Perhaps you're confusing it with the native Lua function of string.match()? Note the lower case difference.

        2nd problem:
        If a valid pattern has been defined, a 'greedy' match should be applied. This means using string.gmatch(), not string.match(). When defining the pattern, the ? symbol is a modifier for an optional character from another character-class. And can only be applied to a character-class. Study the material linked to above, first. The long way is often the short way.

        3rd problem:
        You still have not supplied the full code snippet which is giving you trouble. I'm not a mind reader. How are you storing the data for the chapter numbers? As individual strings? As a table? Or series of tables? I need the code context within which you're attempting the action.

        You say, you have it working up until the point where it reaches "5x000_24 Hours.avi", right? Well then, show me the full code block you're using, so I can see where you're going wrong. So, show me the DATA set you're attempting to work with and the FUNCTION you're attempting to apply to it - in fully coded format.

        Example:
        Code:
        [COLOR=#008000][I]--    a local table containing the DATA set[/I][/COLOR]
        local t = {
            "1x01_24 Hours.avi",
            "4x001_24 Hours.avi",
            "5x002_24 Hours.avi"
        };
        
        [COLOR=#008000][I]-- one possible FUNCTION we can apply to extract the relevant data[/I][/COLOR]
        for k,v in pairs (t) do
            for substring in v:gmatch ("[0]%d[%d]?") do 
                Dialog.Message("Chapter No.", substring)
            end
        end
        Explanation
        v:gmatch() - is the shorthand version of the native Lua function: string.gmatch(). Note the lower case - do not confuse native Lua functions with AMS functions.

        [0]%d[%d]? - This is a Lua pattern that will match for any 2 or 3 digit numbers that begin with 0. Note the ? symbol is applied as an optional character for the character-class %d (digits from 0-9) and is seperated by square [] brackets.

        Comment


        • #5
          BioHazard the names of the chapters are exactly the way you represent them. I did it the way you suggested and it no longer has failures, as you said, it seems that the error was due to the use of match instead of gmatch, here I send what I consider to be the fragment of code necessary to interpret in a correctly way, what I am doing, if not my apologies, I am very new, I appreciate your help and may be that I continue with the subject of patterns later on, I still do not finish my project, but not before studying the materials that you suggest, again thanks.

          Code:
          tblchap = {}
          dirconten = {}
          -- getting the conten of my series directory named SERIES 2, I send a txt with the resultant table series2
          series2 = Folder.Find( "E:\\4000\\SERIES 2\\", "*", false, nil);
          TextFile.WriteFromTable("E:\\SERIES 2.txt", series2, false);    
          f = table.maxn(series2)
          
          -- gettin de number of chapters of each folder in my series directory, in chapter count.txt a check the result for each folder at the time
          -- that to check if is the desired result, and is it!!!!!
          for dir = 1, f, 1 do
          dirconten = File.Find(series2[dir].."\\", "*.*", false, false);
              for k, v in pairs (dirconten) do   
                  for substring in v:gmatch ("(%d%d%d?)_") do
                  tblchap[k] = substring
                  end
                  TextFile.WriteFromTable("E:\\chapter count.txt", tblchap, false);    
              end
          tblchap = {}
          end

          Comment


          • #6
            Upload Attachmens do not working for me, sorry if there is another way please tell me

            Comment

            Working...
            X