【问题标题】:Pattern to match all lowercase letters in strings匹配字符串中所有小写字母的模式
【发布时间】:2019-04-22 20:06:52
【问题描述】:

我对模式很陌生。我想接jjjjj、ae和hii的字符串。

我想匹配每个带引号的字符组中的所有小写字母。

Str = [["1jjjjj" "Wae1" "Uhi2i"]]
for X in string.gmatch(Str,'%".-[%l]([%l]*).-%"') do
    print(X) -- jjjjj, ae, hii
end

【问题讨论】:

  • 请清晰准确。在测试中你说你想要 jjjjj 和 hii,但是在代码中你说你想要打印 jjj 和 i。你想要什么?
  • 我想打印括号内单词的所有小写字母

标签: lua


【解决方案1】:

给你。至少有一种方法:

local str = [["1jjjjj" "Wae1" "Uhi2i"]]
for word in str:gmatch("%S+") do    
    local lowercase = ""

    for char in word:gmatch("%l") do
        lowercase = lowercase .. char
    end

    print(lowercase)
end

【讨论】:

  • 给你。至少有一种方法可以做到这一点: local str = [["1jjjjj" sjaj "Wae1" jaka "Uhi2i"]] for word in str:gmatch("%S+") do local lowercase = "" for char in word: gmatch("%l") do lowercase = lowercase .. char end print(lowercase) end //所以可能是错误的,因为它会占用更多的 " 内的字母
  • 是的,这一切听起来都很熟悉;)如果对您有用,请将答案标记为正确。
【解决方案2】:

如果您想保留小写字母,您可以删除 非小写字符。

xs = {"1jjjjj", "Wae1", "Uhi2i"}
for _, x in ipairs(xs) do
    lowers = x:gsub("%L", "")
    print(lowers)
end
jjjjj
ae
hii

我假设您可以灵活地使用表来存储字符串,而不必解析单个字符串的引号部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 2011-01-16
    相关资源
    最近更新 更多