当最后一行缺少 0 时,Lua 模式无法正常工作
下面是我的模式,它对给定的字符串运行良好。
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
但是当我从下面提到的字符串的最后一行中删除 0 时。模式不匹配。任何帮助将不胜感激。
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
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简而言之,标点符号之前的数字在您的模式中不是可选的。只需添加
*
即可匹配尽可能多的数字,但也不允许包含任何数字。如果您只想匹配单个数字或不匹配数字,但不想匹配之前的任何其他数字,则另一个选项是使用?
。请注意,除此之外,您可能还需要考虑其他一些改进。例如,这将完全忽略该数字,因为之前的
.-
将包含它,将标点符号更改为仅允许.
,并稍微更改换行要求:有关模式的更多详细信息,请参阅Lua 编程。
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.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:See Programming in Lua for more detail on patterns.
您的模式需要小数点前的数字:
此外,您可能需要更合理的捕获:
Your pattern requires a number before the decimal point:
additionally, you may want to more sensible captures: