bohr
bohr

Reputation: 661

How can I split a sentence based on underscore in Java

I am currently dealing with text processing and get distracted by a simple problem. I tried to split the sentence based on an underscore. Firstly, I came up with a very simple approach:

String[] tokens = taggedSentence.split("_");

It works fine for most cases until I found the text that was written like:

Robert_Phd_NNP

However, i am only interested with the POS tag, which is in this case is NNP, but it seems splitting with underscore alone is not enough. Then, I came up with an idea to get the last element of the split sentence:

String[] tokens = taggedSentence.split("_");
int tokenSize = tokens.length;
String pos = tokens[tokenSize-1];

However, I wonder if there is a better way (like regex, maybe) to do this. Any comment or suggestion would be really appreciated. Many Thanks

Upvotes: 2

Views: 2537

Answers (1)

Sean Bright
Sean Bright

Reputation: 120654

You can do this easily without regular expressions:

String POStag = taggedSentence.substring(taggedSentence.lastIndexOf('_') + 1);

If there is no underscore, this will just return the original string.

Upvotes: 6

Related Questions