Trying to parse out names with given samples
++++++++++++++++++SELIZABETH+COLLAZO+++++++++++++++++++
+++++++++++++++++++PALOMA+CORREA+++++++++++++++++++++++
+++++++++++++++++++NOAH+BLAKEMORE++++++++++++++++++++++
I've tried
//++(.*?)+(.*?)//++
but that's way off.
Would like to parse out the first and last name to two strings.
Youcef LAIDANI
60.3k21 gold badges112 silver badges178 bronze badges
asked Nov 7, 2017 at 15:03
Zachary Loughridge
1771 silver badge10 bronze badges
1 Answer 1
You can use this regex (\w+)\+(\w+) or \+{2,}(.*?)\+(.*?)\+{2,} with Pattern like this :
String str = "++++++++++++++++++SELIZABETH+COLLAZO+++++++++++++++++++\n"
+ "+++++++++++++++++++PALOMA+CORREA+++++++++++++++++++++++\n"
+ "+++++++++++++++++++NOAH+BLAKEMORE++++++++++++++++++++++";
Pattern pattern = Pattern.compile("(\\w+)\\+(\\w+)");// or instead "\\+{2,}(.*?)\\+"(.*?)\\+{2,}
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1) + " " + matcher.group(2));
}
Outputs
SELIZABETH COLLAZO
PALOMA CORREA
NOAH BLAKEMORE
answered Nov 7, 2017 at 15:09
Youcef LAIDANI
60.3k21 gold badges112 silver badges178 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Youcef LAIDANI
Hi down-voter, please put a comment when you down-vote a post, it is not legal to down-vote just like this, you have to respect the efforts of others, because we respect your efforts also. Thank you.
lang-java
+?//in your regex?\w+?+with\\instead of using//, and also to escape the one in the middle?+, you should escape it with\\in the Java string literal