0

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

Shax
  • 4,207
  • 10
  • 46
  • 62

2 Answers2

1

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.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • @B Mitch, It seems your answer is working fine. I will do some more testing against your provided pattern. I must say Thank you for your wonderful support. – Shax Jun 03 '11 at 13:12
0

Your pattern requires a number before the decimal point:

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

additionally, you may want to more sensible captures:

paytternToMatch = "(%w%w%w[%w%d][%w%d])%-.-(%d*%p%d%d)\n[\n]+"
daurnimator
  • 4,091
  • 18
  • 34
  • paytternToMatch = "(%w%w%w[%w%d][%w%d])%-.-(%d*%p%d%d)\n[\n]+" not working, it is only fetching "ABC01" – Shax Jun 06 '11 at 09:06