1
local invoiceData =
[[I N V O I C E
Invoice No. :
ABCDEFG125469857
Invoice Date May
2012
]]

The pattern I am using is

 print (string.match(invoiceData,'\nInvoice Date (.-)\n'))

I want to fetch the string invoice date as MAY12. or 0512.. please help

Thanks

1 Answer 1

2

Instead of matching with .-, be more specific and use %w+ (alpha-nums) and %d+ (digits) to match the month and year.

The script:

local invoiceData =
[[I N V O I C E
Invoice No. :
ABCDEFG125469857
Invoice Date May
2012
]]

month, year = string.match(invoiceData,'Invoice%s+Date%s+(%w+)%s+%d*(%d%d)')
print(month, year)

will print:

May    12
Sign up to request clarification or add additional context in comments.

2 Comments

The OP does not want the century. So this use %d%d(%d%d) in the last part. OTOH, your pattern is more robust just in case.
@lhf, good point. I adjusted my pattern from (%d+) to %d*(%d%d) which will match both the input "... 2012" and "... 12" as "12"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.