【问题标题】:Detect word at the end of the sentence with regex使用正则表达式检测句子末尾的单词
【发布时间】:2018-07-17 21:11:51
【问题描述】:

我从正则表达式开始。我的字符串可能看起来像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 

【问题讨论】:

  • 尝试转义 .在第二个示例中,还在末尾附加一个 $ 以仅匹配行尾
  • @AlexanderDaum:像这样:ngx.var.host:match("(.*).nexus$") == nil
  • 我不知道 lua 或 nginx 正则表达式的实现,但你可能必须写 %.代替 。匹配字符。而不是任何字符

标签: regex linux nginx lua


【解决方案1】:

你可以使用

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 子字符串
  • $ - 字符串结束。

【讨论】:

    猜你喜欢
    • 2016-12-22
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    相关资源
    最近更新 更多