'How do I match a string with "-" in it with LUA?
I am trying to match text in between two strings. So far, this works well on 'find1', but 'find2' produces an error.
Example1:
find1 = cmd:match("<model>(.*)</model>") or "ERROR"
find2 = cmd:match("<sw-version>(.*)</sw-version>") or "ERROR"
print(find1, find2)
Result:
model
ERROR
Example2:
find1 = cmd:match("<model>(.*)</model>") or "ERROR"
find2 = cmd:match("<sw\-version>(.*)</sw\-version>") or "ERROR"
print(find1, find2)
Result:
model
ERROR
I am using and can only use native LUA. I believe my code is fine for the purpose I need. I am just wondering if "-" is an operator and/or if "-" is interpreting it as a range? If so, how do I fix it? I've tried it with the "\"
escape character and it has not fixed it.
Solution 1:[1]
You can use %
symbol to escape special characters, (including %
itself), so using cmd:match("<sw%-version>(.*)</sw%-version>") or "ERROR"
should work. See the manual for further details.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Paul Kulchenko |