I have to match /home/user/file.lib
both in
bibliography: /home/user/file.lib
as well as
bibliography: "/home/user/file.lib"
in Lua.
The best I came up with is:
> s1 = "bibliography: /home/user/file.lib"
> s2 = 'bibliography: "/home/user/file.lib"'
> string.match(s1, 'bibliography: "?(%g+)"?')
/home/user/file.lib
> string.match(s2, 'bibliography: "?(%g+)"?')
/home/user/file.lib"
But the last match contain a trailing "
.
It looks like I need a non-greedy version of the +
operator, and it looks like Lua doesn't have it.
How can I achieve my purpose?
Since %g+
matches one or more printable chars and "
is also a printable char. So, you need to match any printable characters other than "
:
s1 = "bibliography: /home/user/file.lib"
s2 = 'bibliography: "/home/user/file.lib"'
print(string.match(s1, 'bibliography: "?([^%G"]+)"?'))
-- /home/user/file.lib
print(string.match(s2, 'bibliography: "?([^%G"]+)"?'))
-- /home/user/file.lib
See the Lua demo.
The [^%G"]+
pattern matches one or more characters (+
) other than non-printable chars (%G
) and "
.