regex - Split string by keeping "-" in words while eliminating it in other places in Java -


i want split string str = "hello, ,, one-word. yes - no: yea?"

string[] parts = [hello, one-word, yes, no, yea] 

so far, have used str.split("(\\p{punct}*\\s)+")) gives parts = [hello, one, word, yes, no, yea], , str.split("[\\p{punct}&&[^-]]*\\s")) gives parts = [hello, one, -, word, yes, -, no, yea].

how split str, keeping -s , _s in words eliminating them , other regex in other places? want eliminate cases of multiple punctuation , white space, such ., , ,.

you can split using regex in java:

"\\s+-\\s+|(?:(?!-)[\\s\\p{punct}])+" 

regex demo

code demo


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -