How to get a particular word/string in lua regex

754 Views Asked by At

I have a problem to get a string. Here is my code:

conf = "option fn_o 'Operator'"
print(conf)
local s, e, pa = string.find(conf, "\b(?!option|fn_o)\b\w+")
print(s, e, pa)

I want to get an Operator only. In Javascript, that regex works good, but in Lua it doesn't. I think there is no problem because Lua is based on json so it is similar to javascript. Is there any problem?

1

There are 1 best solutions below

0
On

Lua does not support full regular expressions out of the box, but there are libraries that do.

Lua includes patterns, which suffice for your task.

This code gets the string inside single quotes:

print(string.match(conf, "'(.-)'"))

The pattern reads: find a single quote and capture everything until the next single quote.