2

I have some string data that I need to search through to find a specific number:

Here's a sample 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

What I need to do is search through this output and the 4 digits that follow "Conference 10-". In this case it is 1234 that I'm after.

**What I've tried **

I've tried all the following patterns... none of which are giving me what I need:

  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));
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Happydevdays
  • 1,982
  • 5
  • 31
  • 57

2 Answers2

1

You need to escape the hyphen with % as unescaped - is a lazy quantifier in Lua (-   also 0 or more repetitions).

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)') )
                                      ^

This will print 1234.

From the 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.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

Use gsub():

print(string.gsub(".*Conference 10%-(%d%d%d%d)%-.*", "%1"));
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    @stribizhev already fixed :) ( just reading the manual now - I wonder why it was felt necessary to not use standard regex?) – Bohemian Nov 26 '15 at 21:19
  • There is no point in using `gsub` since `match` will only return captured substrings if capture groups are defined in the pattern. – Wiktor Stribiżew Nov 26 '15 at 21:20
  • You can read about the limitations in [this answer of mine](http://stackoverflow.com/questions/32884090/amount-of-repetitions-of-symbols-in-lua-pattern-setup/32885308#32885308). – Wiktor Stribiżew Nov 26 '15 at 21:21
  • @str right. I went this way because that's how one would do it in Java (and this is my first attempt at answering a Lua Q). – Bohemian Nov 26 '15 at 21:22