【问题标题】:Please help how to shorten the sentence with LUA pattern请帮助如何使用 LUA 模式缩短句子
【发布时间】:2022-01-06 05:09:54
【问题描述】:

我的模式通过以下方式缩短字符串:

string.gsub("First Second Third", "%s?(.[\128-\191]*)%S+%s", "%1. ")
= "F. S. Third"

我怎样才能获得"First S. T."

非常非常非常感谢大家的回答。

【问题讨论】:

  • string.gsub("First Second Third", "%f[%S](.[\128-\191]*)%S+", "%1.")

标签: lua lua-patterns


【解决方案1】:

我想这就是你想要的

string.gsub("First Second Third", "%s(%a)%a*", " %1.")

返回

First S. T.

如果它需要更像你的模式,它会像这样,返回相同的,至少对于这个例子。但我认为[\128-\191] 部分对我来说没有多大意义

string.gsub("First Second Third", "%s(.[\128-\191]*)%S+", " %1.")

【讨论】:

    【解决方案2】:

    试试这个:

    string.gsub("First Second Third", "(%S+)%s+(%S)%S*%s+(%S)%S*", "%1 %2. %3.")
    

    该模式将单词选择为由空白运行分隔的非空白运行。它捕获第一个单词和其他单词的第一个字母。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多