user3570146
user3570146

Reputation:

Notepad++ RegEx: Delete duplicate lines that are only duplicate from the beginning of the line up to a certain character(s)

Long story short, I am trying to help the Final Fantasy XIV bard performance community by creating a simple voting system which disallows multiple votes from one person. Strawpoll is easy to cheat. ^^ So I've figured out how to do this, but I need help with a RegEx function in Notepad++.

We need to check for when a voter votes multiple times, and delete all those extra votes (and not delete the first vote). So in text terms, everything from the beginning of a line up to >> is what we are checking for duplicates of, and the part after >> in the lines is ignored in the search.

So then this:

VoterName1 >> Thancred
VoterName1 >> Minfilia
VoterName1 >> Thancred
VoterName2 >> Wedge
VoterName3 >> Thancred
VoterName3 >> Wedge
VoterName4 >> Biggs

Will look like this:

VoterName1 >> Thancred
VoterName2 >> Wedge
VoterName3 >> Thancred
VoterName4 >> Biggs

I've tried to find solutions myself by combining things I've seen online and fiddling around and trying to learn RegEx, but programming-thinking (or whatever you would call it) is just not my forte. Anyway, thanks so much for the help!

Upvotes: 1

Views: 661

Answers (2)

Julio
Julio

Reputation: 5308

Tested with notepad++

Search:

  • If usernames can have spaces: ^((.+) >>.+)(\r?\n\2 .+)+
  • If usernames cannot have spaces: ^((\S++).+)(\r?\n\2 .+)+

Replace with: \1

It matches first voter (group 2) and then references it on lines that start with that name. You match all those lines and replace them by first capturing group (the whole first line)

Also, in notepad++ \n does not match \r\n So you usually want to put an extra \r when searching for \n

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 371168

You can use

^(.+) >> (.+)(\n\1.+)+

replace with

$1 >> $2

Output:

VoterName1 >> Thancred
VoterName2 >> Wedge
VoterName3 >> Thancred
VoterName4 >> Biggs

It captures the voter name and the first voted character, then matches subsequent lines with the same voter name via a backreference, and replaces it all with the (single-line) voter name and first voted character.

https://regex101.com/r/8pX4z4/1

Upvotes: 0

Related Questions