我从正则表达式开始。我的字符串可能类似于 nxs_flo.nexus
或 127.0.0.1
或 nxs_flo.nexus.com
我只想过滤 nxs_flo.nexus
类型的字符串。所以我想测试我的字符串中是否有 .nexus
并且它位于字符串的末尾。
这是我过滤 .
的方法,但我不知道如何过滤 .nexus
并且它位于末尾:
if ngx.var.host:match("(.-)%.") == nil then
或者这个用于检测.nexus
,但它不起作用:
if ngx.var.host:match("(.*).nexus") == nil then
最佳答案
您可以使用
local host = [[nxs_flo.nexus]]
if host:match("%.nexus$") == nil then
print("No '.nexus' at the end of the string!")
else
print("There is '.nexus' at the end of the string!")
end
-- => There is '.nexus' at the end of the string!
请参阅online Lua demo .
模式匹配:
%.
- 文字.
charnexus
-nexus
子字符串$
- 字符串结尾。
关于regex - 使用正则表达式检测句子末尾的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48666177/