Kategorieprogram

Pick out mail adresses out of text documents

„I need to write a lot of customers a mail that their trips will be canceled. I only have the adresses saved in a word document, with many additional information. It would take a few hours to copy them hand by hand. Do you have a better idea?“

a client of mine

I had a pretty similar situation a few years back with another client. Back then I searched online for a tool, but couldn’t found one. So I said down and programmed it myself. After the one time use I just forgot about it and over the time I lost it somewhere in my files.

So when I got this request by my client, I first tried to find the file, but gave up short after. So back to programming again.

And due to the lack of a easy tool online I decided to publish the tool myself.

This is just the first, unpolished version. I’ll try to optimize it in the next days, add more features and make it a better user experience.

What does it do?

It asks the user to input the filename of a file on the same level the program is in. It loads it into a string, makes all the characters downcase and removes every line break. Then it searches the file with an RegEx, checks, if the address is already known, and then outputs the mail adresses in the terminal and creates the file/adds them to the file extractedmails.txt in a format, that allows you to just copy paste them into the sending form.

What to change?

I want to optimize the extraction progress, add an options menu for changing the output style, adding a filter and choose the output file. Also I want to add multiple language support.

The Program

The Code

# Mail RegEx
mail = /[a-zA-Z0-9._-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/

# Imports the file as a string and formats it 
print("Bitte gib den Dateinamen der Datei an, die ich nach eMails durchsuchen soll: ")
filename = gets.chop
str = File.read(filename)
str = str.downcase
while str.slice!("\n ") != nil
end

# Uses the RegEx to store any mail in an array
m = mail.match(str)
puts("Alle Mails in der Datei:")
a = []

while m!=nil do
  if not a.include?(m[0]) then
    a.push(m[0])
  end
  str = m.post_match
  m = str.match(mail)
end

# Formats the array into a usable string
res = ""
a.each do |i|
  res = i + ", " + res
end
puts res = res.chop.chop

# Creates an output file
output = File.open( "extractedmails.txt","w" )
output << res
output.close
print "Finished"
gets