0

I am trying to split this string

String string1 = "Employee:\"Timothy Jones\" and Job:\"Senior Management\" and Hired:\"2003\"";

using

String[] splitArray = string1.split("###");

But I am having a lot of trouble with the regex to split it into what I want. My desired result is

employee, Timothy Jones, and, Job, Senior Mangement, and Hired, 2010  

The main problem seems to be the text inside the quotes splitting on the whitespace.

hwnd
  • 69,796
  • 4
  • 95
  • 132
  • 4
    `Employee:”Timothy Jones” and Job:”Senior Management” and Hired:”2003”` is not a valid string in Java. Please format your code and fix it so that it compiles before you post it. – Nir Alfasi Sep 07 '14 at 01:51
  • 1
    this shouldn't even compile... you need to escape the double quotes then wrap the whole string in double quotes. – Mark Giaconia Sep 07 '14 at 01:53
  • What kind of double-quotes is it: `”` or `"` ? – Jonny 5 Sep 07 '14 at 01:58
  • tried str.split("and|[:|\"]"); but i get [Employee, ”Timothy Jones” , Job, ”Senior Management” , Hired, ”2010”] – RedTangerine Sep 07 '14 at 02:02
  • 2
    How come `and, Job,` are separate but `and Hired` is one result? What is your expected result? – hwnd Sep 07 '14 at 02:03

2 Answers2

0

Use matching instead of splitting:

([A-Z][a-z]+ [A-Z][a-z]+|\w+)

A-Z][a-z]+ [A-Z][a-z]+ captures all the phrases like Timothy Jones, Senior Management etc., and \w+ captures the rest of the words.

Note, however, that this regex gives and and Hired as separate matches; I'm not sure why you would want them to be in a single match.

RegEx Demo

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

Or with matching:

".*?"|\S+

".*?" : All phrase surrounded by double quote \S+ : rest of words

walid toumi
  • 2,172
  • 1
  • 13
  • 10