Search code examples
lualua-patterns

Lua pattern is not working properly when 0 is missing in the last row


below is my pattern which is working fine against the given string.

local tempRec = [[

ABC01-USD-0322-A Total DUE amount : 2312.08  USD
Value Date : 31 MAY       2011
   Details:ABCDE - BCD: / ABC01 0212 23.79 / ARM01 0311 870.79
   Details:FGHIJ - BCD: / ABC01 0323 1.88
   Details:KLMNO - BCD: / ABC01 0314 1,035.99
   Details:PQRST - BCD: / ABC01 0315 677.61
   Details:UVWXY - BCD: / ABC01 0316 362.75
   Details:ZABCD - BCD: / ABC01 0317   0.28

   ]]

paytternToMatch = "(%w%w%w[%w%d][%w%d]%-.-%d%p%d%d\n)\n[\n]*"

 for w in string.gmatch(tempRec, paytternToMatch) do
       print(w)
 end

But when I am removing 0 from the last row in the below mentioed string. The pattern is not matching. any help would be appreicated.

local tempRec = [[

ABC01-USD-0322-A Total DUE amount : 2312.08  USD
Value Date : 31 MAY       2011
   Details:ABCDE - BCD: / ABC01 0212 23.79 / ARM01 0311 870.79
   Details:FGHIJ - BCD: / ABC01 0323 1.88
   Details:KLMNO - BCD: / ABC01 0314 1,035.99
   Details:PQRST - BCD: / ABC01 0315 677.61
   Details:UVWXY - BCD: / ABC01 0316 362.75
   Details:ZABCD - BCD: / ABC01 0317   .28

   ]]
paytternToMatch = "(%w%w%w[%w%d][%w%d]%-.-%d%p%d%d\n)\n[\n]*"

 for w in string.gmatch(tempRec, paytternToMatch) do
       print(w)
 end

Thanks


Solution

  • The short answer is that the digit before the punctuation is not optional in your pattern. Simply add a * to match as many digits, but allowing no digits as well. The other option is to use a ? if you only want to match a single or no digits, but not any additional digits before that.

    paytternToMatch = "(%w%w%w[%w%d][%w%d]%-.-%d*%p%d%d\n)\n[\n]*"
    --                                          ^ here
    

    Note that there are several other improvements that you may want to consider in addition to this. For example, this will ignore that digit entirely since the previous .- will include it, change the punctuation to only allow a ., and change the line feed requirement a bit:

    paytternToMatch = "(%w%w%w[%w%d][%w%d]%-.-%.%d%d\n)\n+"
    

    See Programming in Lua for more detail on patterns.