mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jan 2021
Posts: 10
B
Pikka bird
OP Offline
Pikka bird
B
Joined: Jan 2021
Posts: 10
I want to delete repeated words with Reg(sub)ex?
Example: Nickname1 Nickname2 Nickname3 Nick1
Return: Nickname1 Nickname2 Nickname3

Joined: Nov 2021
Posts: 96
Babel fish
Offline
Babel fish
Joined: Nov 2021
Posts: 96
this seems to work for me:

Code

$regsubex($1-,/(^|\s+)(\S+)(($|\s+)\2)+/Sgi,$null)  


Joined: Jul 2006
Posts: 4,153
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,153
Simo's code doesn't work correctly, your example is also incorrect as it doesn't contain the same word twice. Here is something that works, it deletes from the left:

//echo -a $regsubex(Nickname1 Nickname1 Nickname2 Nickname3 Nickname2 Nickname1,/(?:\G| )([^ ]+) (?=(?:\1|.* \1)(?: |$))/gSi,)
Quote
Nickname3 Nickname2 Nickname1


#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Jan 2021
Posts: 10
B
Pikka bird
OP Offline
Pikka bird
B
Joined: Jan 2021
Posts: 10
Originally Posted by Wims
Simo's code doesn't work correctly, your example is also incorrect as it doesn't contain the same word twice. Here is something that works, it deletes from the left:

//echo -a $regsubex(Nickname1 Nickname1 Nickname2 Nickname3 Nickname2 Nickname1,/(?:\G| )([^ ]+) (?=(?:\1|.* \1)(?: |$))/gSi,)
Quote
Nickname3 Nickname2 Nickname1

Hi @Wims

While deleting the second repetition, it merges it with the other value. See;

//echo -a $regsubex(Nickname1 Nickname3 NickName2 Nickname2 Nickname1,/(?:\G| )([^ ]+) (?=(?:\1|.* \1)(?: |$))/gSi,)


Return: Nickname3Nickname2 Nickname1

Joined: Nov 2021
Posts: 96
Babel fish
Offline
Babel fish
Joined: Nov 2021
Posts: 96
try this :

Code

//echo -a $regsubex(Nickname1 Nickname1 Nickname2 Nickname3 Nickname2 Nickname1,/(?:\G| )([^ ]+) (?=(?:\1|.* \1)(?: |$))/gSi,$chr(32))


Joined: Jul 2006
Posts: 4,153
W
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 4,153
Nice catch, here is a simple fix:

//echo -a $regsubex(Nickname1 Nickname3 NickName2 Nickname2 Nickname1,/(?<=\G| )([^ ]+) (?=(?:\1|.* \1)(?: |$))/gSi,)

@Simo: you're replacing the match with a space, but we're trying to get rid of extra space when deleting a word, not add more, it was deleting a space incorrectly because the first part (?:\G| ) consume the space incorrectly on the left of the word, by making this part a positive lookbehind, it's no longer consumed. Note that it's possible to deal with this with a sexy \K as well

//echo -a $regsubex(Nickname1 Nickname3 NickName2 Nickname2 Nickname1,/(?:\G| )\K([^ ]+) (?=(?:\1|.* \1)(?: |$))/gSi,)In fact this \K technique is required with PCRE, had OP's requirement be to match non fixed length stuff.

Last edited by Wims; 05/05/24 05:32 PM.

#mircscripting @ irc.swiftirc.net == the best mIRC help channel
Joined: Nov 2021
Posts: 96
Babel fish
Offline
Babel fish
Joined: Nov 2021
Posts: 96
excellent thanks for the explanation and clarification Wims as you noticed im by no means a wizard of regex.


Link Copied to Clipboard