I have data that is reliably in this format:
1. New York Times - USA
2. Guardian - UK
3. Le Monde - France
I’m using this code to parse out the newspaper and country values:
String newspaper = "";
String country = "";
int hyphenIndex = unparsedText.indexOf("-");
if (hyphenIndex > -1)
{
newspaper = unparsedText.substring(0, hyphenIndex);
}
country = unparsedText.substring(hyphenIndex + 1, unparsedText.length());
country = country.trim();
But this produces newspaper values of:
1. New York Times
2. Guardian
3. Le Monde
What’s the simplest change to make to end up with newspaper values of:
New York Times
Guardian
Le Monde
Here is a regex based solution:
The regex works in multiline mode
(?m)and deletes:followed by any number of space.
I’m assuming there are no hyphens in the newspaper name.
Code In Action