How to find duplicate string matching patterns?

I have a line like this:

[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 Training DummyDungeoneer Training Dummy 33265 Nature. 

In case you are interested, this is from World of Warcraft.

I would like to end up with something like this:

 [13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer Training Dummy 33265 Nature. 

If you notice, "Dummoneer Training Dummy" is printed twice. I managed to get rid of the first part of "| Hunit" with something like this:

 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 Training DummyDungeoneer Training Dummy 33265 Nature." str = string.gsub(str, "|Hunit:.*:.*Your", "Your") 

What returns this:

 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 Training DummyDungeoneer Training Dummy 33265 Nature. 

Then I add a second gsub:

 str = string.gsub(str, "|Hunit:.*:", "") print(str) # => [13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer Training DummyDungeoneer Training Dummy 33265 Nature. 

But the double line "Dungeineer Training Dummy" is repeating, obviously.

How can I get rid of a duplicate string? This line can be anything, in this case "Dungeineer Training Dummy", but it can be the name of any other target.

+7
lua world-of-warcraft lua-patterns
source share
1 answer

You can try something like this:

 str = "[13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer Training DummyDungeoneer 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) 

Fingerprints: [13:41:25] [100:Devnull]: 01:41:20, 13:41:21> Your [Chimaera Shot] hit Dungeoneer Training Dummy

This code will try to extract the target name and check if the name is a complete duplicate. If the match fails, it returns the original string.

+4
source share

All Articles