How would I extract in Lua a text between a pattern. For example
s="this is a test string. <!2014-05-03 23:12:08!> something more"
2014-05-03 23:12:08
print(string.gsub(s, "%<!.-%!>"))
doesn't work"this is a
test string. something more"
The pattern "<!.-!>"
works, but you need to use string.match
to get the date/time part:
print(string.match(s, "<!(.-)!>"))
Note that you don't need to escape !
or <
in a pattern. Of course escaping them is not an error.
To get the string without the date/time part, replace it with an empty string:
local result = string.gsub(s, "<!.-!>", "")
print(result)
You can also expand the pattern .-
to validate the format of date/time more:
result = string.gsub(s, "<!%d%d%d%d%-%d%d%-%d%d%s+%d%d:%d%d:%d%d!>", "")
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