问题描述:

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

网友答案:

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.

相关阅读:
Top