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 ".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With