3

I want to extract the VALUE of lines containing key="VALUE", and I am trying to use a simple Lua pattern to solve this.

It works for lines except for those which contains a literal 1 in the VALUE. It seems the pattern parser is confusing my capture group for an escape sequence.

> return  string.find('... key = "PHONE2" ...', 'key%s*=%s*(["\'])([^%1]-)%1')
5       18      "       PHONE2
> return  string.find('... key = "PHONE1" ...', 'key%s*=%s*(["\'])([^%1]-)%1')
nil
>
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
nolan
  • 93
  • 7

1 Answers1

1

You do not need to use the [^%1] at all. Just use .- as it, by definition, matches the smallest possible string.

Also, you can use multiline string syntax, to not have to escape the quotes in your pattern:

> s=[[... key = "PHONE1" ...]]
> return s:find [[key%s*=%s*(["'])(.-)%1]]
5       18      "       PHONE1

The pattern [^%1] actually means, do not search for characters % and 1 individually.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Thanks. I find out as well that I would escape the 1 to force it to include the capture group in the set. `[^%\1]` Your solution makes more sense and is cleaner. I will use it. – nolan Sep 01 '16 at 07:47
  • @nolan In that case, you just want to use `[^\1]`. The `%` character is not required. – hjpotter92 Sep 01 '16 at 07:48
  • Indeed it is not. Thank you. – nolan Sep 01 '16 at 07:53