88145

Extracting Names and Email address from String with regex

I have been trying to extract the names and email addresses from the following String that consists of multiple lines through regex in Java:

From: Kane Smith <Kane@smith.com> To: John Smith <john@smith.com>, Janes Smith <jane@smith.org>, Tom Barter <tom@test.co.uk>, Other Weird @#$@<>#^Names <other@names.me>, Long Long Long Long Name <longlong@name.com> Date: Tue, 25 Oct 2011 15:45:59 +0000

I tried this regex: To:\s?(([.*]+)\s*<([\w\d@\.]*)>,(\s|\n)*)+ But it doesn't work.

My intention is to extract each of the names and email addresses and put each name its email address together into groups. What I have done however, seems to work only when there is one single name and address. What should my regex be to do this?

Answer1:

String s = "To: John Smith <john@smith.com>, Janes Smith\n" + "<jane@smith.org>, Tom Barter <tom@test.co.uk>, Other \n" + "Weird @#$@<>#^Names <other@names.me>, \n" + "Long Long Long Long Name <longlong@name.com>"; s = s.substring(3); // filter TO: System.out.println(s); // Use DOTALL pattern Pattern p = Pattern.compile("(.*?)<([^>]+)>\\s*,?",Pattern.DOTALL); Matcher m = p.matcher(s); while(m.find()) { // filter newline String name = m.group(1).replaceAll("[\\n\\r]+", ""); String email = m.group(2).replaceAll("[\\n\\r]+", ""); System.out.println(name + " -> " + email); }

Answer2:

you can split each line on "," and then use javax.mail.internet.InternetAddress. That will take care of extracting the name and address.

Btw, where are you getting the headers from and why can't they be key values as they should be?

Recommend

  • Oracle Type Casting
  • Join 3 tables with Count
  • Perl - Using regex to match input in hash key or value
  • Good algorithm/technique to find overlapping values from objects' properties?
  • Rspec doesn't reload changed password attribute - why?
  • How to create mentions for names like “@myname” using javascript?
  • Generate c# object code and assign values to its properties from an xml document
  • What are the best practices for migrating an Oracle 10g database to Microsoft SQL 2008 R2? Applicati
  • xquery to return the element name
  • SQL: Query for a group that contains an exact set of users
  • How can I get rid of dynamic SQL
  • Haskell: function composition with anonymous/lambda function
  • how can I compare dates in array to find the earliest one?
  • Count from each distinct date, fill in missing dates with zero
  • How to validate a year I enter in textbox using jquery rule?
  • How to request unsampled report from Google Analytics Management API using Google Apps Script?
  • Identifier too long in Oracle
  • Creating a Multidimensional, Associative Array in VBScript
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • Easiest way to get current unix timestamp via XSL
  • HttpListener.IsSupported is false on XP SP3
  • How to get data from **Realm database** using **date object**?
  • Saving image to sd with current date and time in name doesn't work
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Combining two different ActiveRecord collections into one
  • Stop Bash Script if Hive Fails
  • Why querying a date BC is changed to AD in Java?
  • Row Count Is Returning the incorrect number using RaptureXML
  • Highlight one bar in a series in highcharts?
  • Cannot connect to cassandra from Spark
  • Display issues when we change from one jquery mobile page to another in firefox
  • Illegal mix of collations for operation for date/time comparison
  • Deselecting radio buttons while keeping the View Model in synch
  • Cross-Platform Protobuf Serialization
  • JSON with duplicate key names losing information when parsed
  • Release, debug version and Authorization Google?
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • Jquery - Jquery Wysiwyg return html as a string
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Hits per day in Google Big Query