Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 尝试使用Lua匹配domain\username格式的字符串,然后使用'#';_Regex_Lua_Lua Patterns - Fatal编程技术网

Regex 尝试使用Lua匹配domain\username格式的字符串,然后使用'#';

Regex 尝试使用Lua匹配domain\username格式的字符串,然后使用'#';,regex,lua,lua-patterns,Regex,Lua,Lua Patterns,我正在尝试使用Lua匹配domain\username格式的字符串,然后用#屏蔽该模式 因此,如果输入是sample.com\admin输出应该是。字符串可以以结尾、、、或空白 更多示例: sample.net\user1,hello -> ######.###\#####,hello test.org\testuser. Next -> ####.###\########. Next 我试过([a-zA-Z][a-zA-Z0-9.-]+)\?([a-zA-Z0-9]

我正在尝试使用Lua匹配domain\username格式的字符串,然后用
#
屏蔽该模式

因此,如果输入是
sample.com\admin输出应该是
。字符串可以以
结尾
或空白

更多示例:

sample.net\user1,hello   ->   ######.###\#####,hello
test.org\testuser. Next  ->   ####.###\########. Next
我试过
([a-zA-Z][a-zA-Z0-9.-]+)\?([a-zA-Z0-9]+)\([a-zA-Z0-9]+)\b
,它可以很好地使用。但Lua演示版却没有。这种模式有什么问题

下面是我用来签入Lua的代码:

test_text="I have the 123 name as domain.com\admin as 172.19.202.52 the credentials"
pattern="([a-zA-Z][a-zA-Z0-9.-]+).?([a-zA-Z0-9]+)\\([a-zA-Z0-9 ]+)\b"
res=string.match(test_text,pattern)
print (res)

它正在打印
nil

Lua模式不是正则表达式,这就是正则表达式不工作的原因

  • \b
    不受支持,如果需要,可以使用更强大的
    %f
    前沿模式
  • 在字符串
    test\u text
    中,
    \
    没有转义,因此它被解释为
    \a
  • 是模式中的魔法字符,需要对其进行转义
此代码与您的模式不完全相同,如果需要,您可以查看它:

test_text = "I have the 123 name as domain.com\\admin as 172.19.202.52 the credentials"
pattern = "(%a%w+)%.?(%w+)\\([%w]+)"
print(string.match(test_text,pattern))
输出:
domain com admin



修复模式后,用
#
替换它们的任务很简单,您可能需要
string.sub
string.gsub
就像前面提到的纯Lua没有正则表达式,只有模式。 但是,您的正则表达式可以与以下代码和模式匹配:

--[[
sample.net\user1,hello   ->   ######.###\#####,hello
test.org\testuser. Next  ->   ####.###\########. Next
]]

s1 = [[sample.net\user1,hello]]
s2 = [[test.org\testuser. Next]]
s3 = [[abc.domain.org\user1]]

function mask_domain(s)
  s = s:gsub('(%a[%a%d%.%-]-)%.?([%a%d]+)\\([%a%d]+)([%;%,%.%s]?)',
      function(a,b,c,d)
        return ('#'):rep(#a)..'.'..('#'):rep(#b)..'\\'..('#'):rep(#c)..d
      end)
  return s
end

print(s1,'=>',mask_domain(s1))
print(s2,'=>',mask_domain(s2))
print(s3,'=>',mask_domain(s3))
最后一个示例不是以。或者空白。如果必须遵循这一点,那么只需删除最后一个?从模式

更新:如果在域(例如abc.domain.org)中,您还需要显示最后一个点之前的任何点,您可以使用此点替换上述功能:

function mask_domain(s)
  s = s:gsub('(%a[%a%d%.%-]-)%.?([%a%d]+)\\([%a%d]+)([%;%,%.%s]?)',
      function(a,b,c,d)
        a = a:gsub('[^%.]','#')
        return a..'.'..('#'):rep(#b)..'\\'..('#'):rep(#c)..d
      end)
  return s
end

lua不支持正则表达式。lua使用。如果你需要正则表达式,你需要为他们提供一个库。你可能想考虑使用它。它没有正确地为ABC。域名。org \USE1,它只提供域org UsR1作为输出。