0

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.

7
  • how about a split on + ? Commented Nov 7, 2017 at 15:05
  • Why are you matching // in your regex? Commented Nov 7, 2017 at 15:06
  • Why don't you just do \w+? Commented Nov 7, 2017 at 15:06
  • Did you mean to escape the + with \\ instead of using //, and also to escape the one in the middle? Commented Nov 7, 2017 at 15:07
  • To match a literal +, you should escape it with \\ in the Java string literal Commented Nov 7, 2017 at 15:09

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.