You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lua中string.match校验字符串仅含字母数字异常问题求助

Lua字符串校验问题:判断仅含字母数字始终进入错误分支

我是Lua新手,现在要校验MAC地址文件首行的字符串是否仅包含字母数字字符,使用string.match函数判断返回值,但无论字符串是否合法,都会进入错误分支(else块)。以下是我的代码:

function ReadFistMacAddressFile (_FilePath)

    local file = open(_FilePath, "rb") -- r read mode and b binary mode
    if not file then
        file:close()
        appli.Test_failed("Can't open the file".. _FilePath .. ".

 Verify the path.")
        return nil
    end
    
    print("------------------------")
    print("-------------------------")
    local size = sizeFile (file)
    print("     size = " .. size)
    print("")
    print("")
    if size == 0 then
        file:close()
        appli.Test_failed("Mac Adress file's empty")
        return nil
    end
        
    
    local lignes = ReadLine (file, 1)
    print("     lignes = " .. lignes)
    print("")
    print("")
    
    local NoneAlphaNumericFind = nil 
    NoneAlphaNumericFind = string.match(lignes,"[^%w]")
    print("     NoneAlphaNumericFind = " .. NoneAlphaNumericFind)
    print("")
    print("")
    
    if NoneAlphaNumericFind == nil or NoneAlphaNumericFind == '' then
        file:close()
        return lignes
    else
        file:close()
        appli.Test_failed("Mac adress contain the characteres: ".. NoneAlphaNumericFind)    
        return nil 
    end
    
end

问题原因

  1. 隐藏的空白/换行符:ReadLine读取的行大概率包含换行符(\n)或回车符(\r),这些字符不属于%w(字母数字)范畴,会被[^%w]匹配到,导致进入错误分支。
  2. 判断逻辑冗余:string.match未匹配到结果时只会返回nil,不会返回空字符串,所以NoneAlphaNumericFind == ''的判断完全多余,且不影响结果。
  3. 文件打开失败的错误处理:当open返回nil时,调用file:close()会直接报错,因为此时file是nil,没有close方法。

解决方案

  1. 去除字符串两端空白:先处理读取到的行,去掉两端的换行、空格、制表符等空白字符。
  2. 优化校验逻辑:直接校验整个字符串是否从开头到结尾都是字母数字(使用^%w+$模式),更准确。
  3. 修复错误处理逻辑:文件打开失败时不要执行file:close()。

修改后的完整代码

-- 辅助函数:去除字符串两端的空白字符(包括换行、空格等)
local function trim(s)
  return s:match'^%s*(.*%S)' or ''
end

function ReadFistMacAddressFile (_FilePath)
    local file = open(_FilePath, "rb") -- r read mode and b binary mode
    if not file then
        appli.Test_failed("Can't open the file ".. _FilePath .. ". Verify the path.")
        return nil
    end
    
    print("------------------------")
    print("-------------------------")
    local size = sizeFile (file)
    print("     size = " .. size)
    print("")
    print("")
    if size == 0 then
        file:close()
        appli.Test_failed("Mac Address file's empty")
        return nil
    end
        
    local lignes = ReadLine (file, 1)
    -- 去除两端空白,避免换行符干扰校验
    lignes = trim(lignes)
    print("     lignes = " .. lignes)
    print("")
    print("")
    
    -- 校验整个字符串是否仅包含字母数字
    local isValid = string.match(lignes, "^%w+$") ~= nil
    if isValid then
        file:close()
        return lignes
    else
        -- 找出第一个非法字符用于提示
        local invalidChar = string.match(lignes, "[^%w]")
        file:close()
        appli.Test_failed("Mac address contains invalid character: ".. invalidChar)    
        return nil 
    end
end

内容的提问来源于stack exchange,提问作者Abdi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:25:52