开发者

Removing duplicates with Regex, using Editpad Lite 7

开发者 https://www.devze.com 2023-04-06 18:12 出处:网络
I have a comma-delimited list of words in a text document. I have basic开发者_运维知识库ally just removed all the punctuation from a novel, so there is a lot of instances of the same words.

I have a comma-delimited list of words in a text document. I have basic开发者_运维知识库ally just removed all the punctuation from a novel, so there is a lot of instances of the same words.

I can't find out how to use regex to leave just one instance of every word.

I'm using Editpad Lite 7.

Could anyone give me advice on how to do this. (If possible at all).


Assuming you have a text that is a comma-delimited list of words like:

hello,world,hello,abc,world

and you want to remove duplicate words so that the resulting text is:

hello,world,abc

I don't think you will be able to do this using regular expressions in Editpad Lite 7. Your best bet is to use a programming language to accomplish this. Here is a simple example using PHP:

$text = "hello,world,hello,abc,world";
$seen = array();
foreach (explode(',', $text) as $word) {
    if (isset($seen[$word])) continue;
    $seen[$word] = true;
    print $word . ',';
}
// Outputs: hello,world,abc,


If your editor supports look-around assertions, you could use it to remove (i.e. replace with "") all matches of

(?<=,|^)([^,]*)(?=,)(?=.*,\1(,|$))

See it in action in RegExr.

The performance of this might be quite bad, depending of the length of the text. If you need to do this regularly, you could be better of writing a small script in the language of your choice.

0

精彩评论

暂无评论...
验证码 换一张
取 消