当最后一行缺少 0 时,Lua 模式无法正常工作

发布于 2024-11-14 05:36:01 字数 1196 浏览 2 评论 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   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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小傻瓜 2024-11-21 05:36:01

简而言之,标点符号之前的数字在您的模式中不是可选的。只需添加 * 即可匹配尽可能多的数字,但也不允许包含任何数字。如果您只想匹配单个数字或不匹配数字,但不想匹配之前的任何其他数字,则另一个选项是使用 ?

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

请注意,除此之外,您可能还需要考虑其他一些改进。例如,这将完全忽略该数字,因为之前的 .- 将包含它,将标点符号更改为仅允许 .,并稍微更改换行要求:

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

有关模式的更多详细信息,请参阅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.

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.

☆獨立☆ 2024-11-21 05:36:01

您的模式需要小数点前的数字:

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

此外,您可能需要更合理的捕获:

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

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]+"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文