0

In my project, I need to extract some parameters from a settings file. Below is a section of the line that I am reading, the parameter I need to extract is the Program Prefix.

...  ProgramPrefix="" ReceiveTimeout="80000" ...

I need to extract what is between the double quotes for ProgramPrefix. The problem is that in-between these quotes can be any alphanumeric character, symbol, space, or no character at all.

Below is my current solution for extracting any character before the second double-quote, the problem doesn't work for the case of anything being between the double quotes

    EOPdefL = string.find(line,"ProgramPostfix=")
    EOPdef = string.match(line,'([^"]+)',EOPdefL+16)

When there is nothing in-between the double quotes the output for EOPdef is:

EOPdef = ReceiveTimeout=

I would like EOPdef to just return an empty string if there are no characters.

EOPdef = ""

EDIT: lhf and Piglet provided a working resolution. The character class to capture 0 or more characters in between double quotes is the following:

'"(.-)"'

Implementing this character class into my solution results in the following code:

SOPdefL = string.find(line,"ProgramPrefix=")
SOPdef = string.match(line,'"(.-)"',SOPdefL+14)
D S
  • 258
  • 9
  • 25
xelarim
  • 1
  • 1

1 Answers1

1

one issue with your pattern is that you're trying to match one or more (+) non-doublequote characters. You need to match 0 or more, shortest match (-).

There are multiple ways to achieve this.

Most obvious as lhf already suggested you capture 0 or more characters between double quotes.

str:match('ProgramPrefix="(.-)"')

or you capture a balanced double quote pair and get its contents

str:match('ProgramPrefix=%b""'):sub(2,-2)
Piglet
  • 27,501
  • 3
  • 20
  • 43