Walkero

software engineer, Drupal follower, Docker ninja, JS explorer, PHP believer, exotic Hardware beta tester, Amiga enthusiast, truth seeker, parent & husband... at least for now...

The editors are my most used programs daily. As a web developer, I use plain text editors for coding. For the last couple of years I prefer to use the jEdit, which is Java based, really fast, with plenty of plugins to make it suitable for you. I use it everyday because it has all the feautures I need from an editor. It also has "macros", which I never used before, until yesterday.

A customer of mine, gave me a big text he created, with plenty of emails written among various other texts. His need was to extract all the emails from the text in a way that he can use to import the emails in a database. So a new text with all the emails, seperated with a semicolon would be just fine.

The jEdit has the ability to search in a text by a regular expression. That would be useful for what I wanted to do. I created one regular expression that recognises an email in a text. That worked just fine. But if I use it manually I should do the following steps over and over again.

Find an email -> Copy it -> switch to an other file -> put a semicolon -> paste it

These steps had to be done for every single email, which would be time demanding for a text like the one I had in front of me. So I thought to use the macros feature of jEdit.

The script I created scans the open buffer text from the beggining, it finds the emails and then creates a new buffer text where it outputs all the emails separated by a semicolon. It also outputs the number of founded emails at the status bar of the editor.

// Extract emails v1.0 (29/2/2012)
// Created by George Sokianos
//
Emails = new StringBuffer();

// Go to the first line of the text
textArea.setCaretPosition(0);

// Set the search preferences
SearchAndReplace.setSearchString("([\\s]*)[\\._a-zA-Z0-9-]+@[\\._a-zA-Z0-9-]+");
SearchAndReplace.setAutoWrapAround(false);
SearchAndReplace.setReverseSearch(false);
SearchAndReplace.setWholeWord(false);
SearchAndReplace.setIgnoreCase(true);
SearchAndReplace.setRegexp(true);
SearchAndReplace.setSearchFileSet(new CurrentBufferSet());

// Serach loop until it stops to find new emails
int count = 0;
while(SearchAndReplace.find(view))
{
    text = textArea.getSelectedText();
    Emails.append(text);
    Emails.append(";");
    count++;
}
// Create a new text file
jEdit.newFile(view);

// Paste all the emails into the new file
textArea.setSelectedText(Emails.toString());

// Inform about the number of the founded emails at the status bar
view.getStatus().setMessageAndClear(String.valueOf(count) + " emails found");
#script #jedit #macros #email extract #editor #emails
- 2 min read