Escape regular expressions on a character

I need to separate a large list of letters and names, I need to separate commas, but some names have commas in them, so I have to deal with this in the first place. Fortunately, the names are between the quotation marks.

At the moment, I get with the release of my regular expression, for example, (editing: it does not display messages on the forum that I see!):

"Talboom, Esther" "Wolde, Jos van der" "Debbie Derksen" < deberken@casema.nl >, corine < corine5@xs4all.nl >, " 

The latter went wrong because the name does not have a comma, so it continues until it finds one, and this is the one I want to use for separation. So I want it to look until it finds a "<". How can i do this?

 import java.util.regex.Pattern; import java.util.regex.Matcher; String test = "\"Talboom, Esther\" < E.Talboom@wegener.nl >, \"Wolde, Jos van der\" < J.vdWolde@wegener.nl >, \"Debbie Derksen\" < deberken@casema.nl >, corine < corine5@xs4all.nl >, \"Markies Aart\" < A.Markies@wegenernieuwsmedia.nl >"; Pattern pattern = Pattern.compile("\".*?,.*?\""); Matcher matcher = pattern.matcher(test); boolean found = false; while (matcher.find ()) { System.out.println(matcher.group()); } 

edit: the best line to work as not everyone has a name or quotation marks:

 String test = "\"Talboom, Esther\" < E.Talboom@wegener.nl >, DRP - Wouter Haan < wouter@drp.eu >, \"Wolde, Jos van der\" < J.vdWolde@wegener.nl >, \"Debbie Derksen\" < deberken@casema.nl >, corine < corine5@xs4all.nl >, clankilllller@gmail.com , \"Markies Aart\" < A.Markies@wegenernieuwsmedia.nl >"; 
+4
source share
1 answer

I would simplify the code using String.split and String.replaceAll . This avoids the problems of working with Pattern and makes the code neat and concise.
Try the following:

 public static void main(String[] args) { String test = "\"Talboom, Esther\" < E.Talboom@wegener.nl >, \"Wolde, Jos van der\" < J.vdWolde@wegener.nl >, \"Debbie Derksen\" < deberken@casema.nl >, corine < corine5@xs4all.nl >, \"Markies Aart\" < A.Markies@wegenernieuwsmedia.nl >"; // Split up into each person details String[] nameEmailPairs = test.split(",\\s*(?=\")"); for (String nameEmailPair : nameEmailPairs) { // Extract exactly the parts you need from the person details String name = nameEmailPair.replaceAll("\"([^\"]+)\".*", "$1"); String email = nameEmailPair.replaceAll(".*<([^>]+).*", "$1"); System.out.println(name + " = " + email); } } 

The output showing it actually works :)

 Talboom, Esther = E.Talboom@wegener.nl Wolde, Jos van der = J.vdWolde@wegener.nl Debbie Derksen = corine5@xs4all.nl Markies Aart = A.Markies@wegenernieuwsmedia.nl 
+2
source

Source: https://habr.com/ru/post/1387814/


All Articles