Lua 匹配 "Conference 10-" 后的 4 位数字的模式

Lua pattern matching 4 digits that follow "Conference 10-"

我有一些字符串数据需要搜索以查找特定数字:

这是一个示例 string/buffer:

Conference 11-2222-a.b.c (1 member rate: 32000 flags: running|answered|enforce_min|dynamic|exit_sound|enter_sound) 176;014802813@mydomain;0182e4e4-193b-4d63-9bef-b597f0655c83;jdo ;014802813;hear|speak|talking|floor;0;0;0;0

Conference 10-1234.c.fdf.c (1 member rate: 32000 flags: running|answered|enforce_min|dynamic|exit_sound|enter_sound)175;.net/4122@mydomain;77c1f301-85e1-4275-9c539e5927b87d6;4122;hear|speak|talking|floor;0;0;0;0

我需要做的是搜索此输出和 "Conference 10-" 后面的 4 位数字。在这种情况下,我要查找的是 1234。

**我试过的**

我已经尝试了以下所有模式...none 其中满足了我的需求:

  print(string.match(input, "10-%d%d%d%d-"));
  print(string.match(input, "Conference 10-%d%d%d%d-"));
  print(string.match(input, "Conference 10-(%d)-");
  print(string.match(input, "Conference 10(\-)(%d));

您需要使用 % 转义连字符,因为 未转义 - 是 Lua 中的惰性量词( - 还有 0 次或多次重复)。

str = "Conference 11-2222-a.b.c (1 member rate: 32000 flags: running|answered|enforce_min|dynamic|exit_sound|enter_sound) 176;014802813@mydomain;0182e4e4-193b-4d63-9bef-b597f0655c83;jdo ;014802813;hear|speak|talking|floor;0;0;0;0\n\nConference 10-1234.c.fdf.c (1 member rate: 32000 flags: running|answered|enforce_min|dynamic|exit_sound|enter_sound)175;.net/4122@mydomain;77c1f301-85e1-4275-9c539e5927b87d6;4122;hear|speak|talking|floor;0;0;0;0"
print(string.match(str, 'Conference 10%-(%d%d%d%d)') )
                                      ^

这个will print 1234.

来自Lua 20.2 – Patterns reference

Some characters, called magic characters, have special meanings when used in a pattern. The magic characters are

( ) . % + - * ? [ ^ $

The character % works as an escape for those magic characters.

使用gsub():

print(string.gsub(".*Conference 10%-(%d%d%d%d)%-.*", "%1"));