如何使用模式匹配查找重复的字符串?
How to find a duplicate string with Pattern Matching?
我有一个类似这样的字符串:
[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> |Hunit:Player-3693-07420299:DevnullYour [Chimaera Shot] hit |Hunit:Creature-0-3693-1116-3-87318-0000881AC4:Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature.
如果你想知道,它来自魔兽世界。
我想以这样的方式结束:
[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer's Training Dummy 33265 Nature.
如果您注意到,"Dungeoneer's Training Dummy" 被打印了两次。我已经设法用这样的东西摆脱了第一个“|Hunit”部分:
str = "[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> |Hunit:Player-3693-07420299:DevnullYour [Chimaera Shot] hit |Hunit:Creature-0-3693-1116-3-87318-0000881AC4:Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature."
str = string.gsub(str, "|Hunit:.*:.*Your", "Your")
哪个returns这个:
print(str) # => [13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit |Hunit:Creature-0-3693-1116-3-87318-0000881AC4:Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature.
然后我添加第二个 gsub:
str = string.gsub(str, "|Hunit:.*:", "")
print(str) # => [13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature.
但是双 "Dungeoneer's Training Dummy" 字符串显然是重复的。
我怎样才能去掉重复的字符串?此字符串可以是任何其他内容,在本例中是 "Dungeoneer's Training Dummy",但它可以是任何其他目标的名称。
您可以尝试这样的操作:
str = "[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature."
-- find a string that starts with 'hit', has some number of non-digits
-- and ends with one or more digit and one or more characters.
-- these characters will be "captured" into three strings,
-- which are then passed to the "replacement" function.
-- the returned result of the function replaces the value in the string.
str = str:gsub("(hit%s+)([^%d]+)(%d+.+)", function(s1, s2, s3)
local s = s2:gsub("%s+$","") -- drop trailing spaces
if #s % 2 == 0 -- has an even number of characters
and s:sub(0, #s / 2) -- first half
== -- is the same
s:sub(#s / 2 + 1) -- as the second half
then -- return the second half
return s1..s:sub(#s / 2 + 1)..' '..s3
else
return s1..s2..s3
end
end)
print(str)
这会打印:[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer's Training Dummy
此代码将尝试提取目标名称并检查该名称是否完全重复。如果匹配失败,它 returns 原始字符串。
我有一个类似这样的字符串:
[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> |Hunit:Player-3693-07420299:DevnullYour [Chimaera Shot] hit |Hunit:Creature-0-3693-1116-3-87318-0000881AC4:Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature.
如果你想知道,它来自魔兽世界。
我想以这样的方式结束:
[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer's Training Dummy 33265 Nature.
如果您注意到,"Dungeoneer's Training Dummy" 被打印了两次。我已经设法用这样的东西摆脱了第一个“|Hunit”部分:
str = "[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> |Hunit:Player-3693-07420299:DevnullYour [Chimaera Shot] hit |Hunit:Creature-0-3693-1116-3-87318-0000881AC4:Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature."
str = string.gsub(str, "|Hunit:.*:.*Your", "Your")
哪个returns这个:
print(str) # => [13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit |Hunit:Creature-0-3693-1116-3-87318-0000881AC4:Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature.
然后我添加第二个 gsub:
str = string.gsub(str, "|Hunit:.*:", "")
print(str) # => [13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature.
但是双 "Dungeoneer's Training Dummy" 字符串显然是重复的。
我怎样才能去掉重复的字符串?此字符串可以是任何其他内容,在本例中是 "Dungeoneer's Training Dummy",但它可以是任何其他目标的名称。
您可以尝试这样的操作:
str = "[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer's Training DummyDungeoneer's Training Dummy 33265 Nature."
-- find a string that starts with 'hit', has some number of non-digits
-- and ends with one or more digit and one or more characters.
-- these characters will be "captured" into three strings,
-- which are then passed to the "replacement" function.
-- the returned result of the function replaces the value in the string.
str = str:gsub("(hit%s+)([^%d]+)(%d+.+)", function(s1, s2, s3)
local s = s2:gsub("%s+$","") -- drop trailing spaces
if #s % 2 == 0 -- has an even number of characters
and s:sub(0, #s / 2) -- first half
== -- is the same
s:sub(#s / 2 + 1) -- as the second half
then -- return the second half
return s1..s:sub(#s / 2 + 1)..' '..s3
else
return s1..s2..s3
end
end)
print(str)
这会打印:[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer's Training Dummy
此代码将尝试提取目标名称并检查该名称是否完全重复。如果匹配失败,它 returns 原始字符串。