java - What's a good regex to remove extraneous characters from the end of a string? -


i'm looking regex remove characters @ end of string aren't needed, may present earlier in string (which expected , good).

here's example should resolve "carver house - kitchen"

carver house - kitchen( carver house - kitchen[ carver house - kitchen - ( carver house - kitchen :  

here's example should resolve "carver house : (night)[n1] - kitchen"

carver house : (night)[n1] -  kitchen( carver house : (night)[n1] -  kitchen[ carver house : (night)[n1] -  kitchen - ( carver house : (night)[n1] -  kitchen :  

so basically, last letter or number should found , else whacked off.

also, i'm not looking big messy method goes character character find last alphanumeric. should possible in line or 2 of code.

thanks

you can use regex \w+$ find non-word-characters @ end of string , replace those.

string s = "carver house : (night)[n1] -  kitchen - ("; s = s.replaceall("\\w+$", ""); system.out.println(s); 

result carver house : (night)[n1] - kitchen


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -