regex - Java Regexp capturing group includes space, why? -


i trying parse string,

"斬釘截鐵 斩钉截铁 [zhan3 ding1 jie2 tie3] /to chop nail , slice iron (idiom)/resolute , decisive/unhesitating/definitely/without doubt/"; 

with code

private static final pattern traditional = pattern.compile("(.*?) "); private string extractsinglepattern(string row, pattern pattern) { matcher matcher = pattern.matcher(row); if (matcher.find()) { return matcher.group(); } return null; } 

however, reason string returned contains space @ end

org.junit.comparisonfailure: expected:<斬釘截鐵[]> was:<斬釘截鐵[ ]> 

is there wrong pattern? have tried

private static final pattern traditional = pattern.compile("(.*?)\\s"); 

but no avail

i have tried matching 2 spaces @ end of pattern, doesn't match (there 1 space).

you're using matcher.group() documented as:

returns input subsequence matched previous match.

the match includes space. capturing group within match doesn't, haven't asked that.

if change return statement to:

return matcher.group(1); 

then believe it'll want.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -