regex - 使用正则表达式检测句子末尾的单词

标签 regex linux nginx lua

我从正则表达式开始。我的字符串可能类似于 nxs_flo.nexus127.0.0.1nxs_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 .

模式匹配:

  • %. - 文字 . char
  • nexus - nexus 子字符串
  • $ - 字符串结尾。

关于regex - 使用正则表达式检测句子末尾的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48666177/

相关文章:

javascript - jQuery 验证中的正则表达式 - jQuery 中的单引号问题

c - 自由RTOS : How to measure context switching time?

nginx - gzip_types image/jpeg 如何工作

ssl - 如何使用 SSL 配置 nginx?

javascript - Meteor客户端证书自动登录

java - 将 EditText 的值设置为自己的值,已修改 - 导致卡住

JavaScript 正则表达式(缺少 '/' ),我该如何解决这个问题?

python - 有什么比在循环中搜索每个子字符串更好的匹配字符串中多个子字符串的方法?

linux - mongodb 将数据保存在两个地方

linux - 制作 : *** [install] Error 1