lua 正则匹配开头和结尾的空字符

    local txt1 = "   a bcde fg   "
    local txt2 = "a bcde fg   "
    local txt3 = "   a bcde fg"
    txt1 = string.gsub(txt1, "^%s*(.-)%s*$", "%1")
    txt2 = string.gsub(txt2, "^%s*(.-)%s*$", "%1")
    txt3 = string.gsub(txt3, "^%s*(.-)%s*$", "%1")
    print("============")
    txt1 = "[start]".. txt1 .."[end]"
    txt2 = "[start]".. txt2 .."[end]"
    txt3 = "[start]".. txt3 .."[end]"
    print(txt1)
    print(txt2)
    print(txt3)
    print("==============")

 

结果如下

 

posted @ 2020-02-06 10:30  HaHahahahaaaa  阅读(1216)  评论(0编辑  收藏  举报