Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java remove all non alphanumeric character from beginning and end of string

I know how to replace ALL non alphanumeric chars in a string but how to do it from just beginning and end of the string?

I need this string:

"theString,"

to be:

theString

replace ALL non alphanumeric chars in a string:

s = s.replaceAll("[^a-zA-Z0-9\\s]", "");
like image 531
Mike6679 Avatar asked Dec 26 '22 06:12

Mike6679


2 Answers

Use ^ (matches at the beginning of the string) and $ (matches at the end) anchors:

s = s.replaceAll("^[^a-zA-Z0-9\\s]+|[^a-zA-Z0-9\\s]+$", "");
like image 56
falsetru Avatar answered Dec 28 '22 05:12

falsetru


Use:

s.replaceAll("^[^\\p{L}^\\p{N}\\s%]+|[^\\p{L}^\\p{N}\\s%]+$", "")

Instead of:

s.replaceAll("^[^a-zA-Z0-9\\s]+|[^a-zA-Z0-9\\s]+$", "")

Where p{L} is any kind of letter from any language.
And p{N}is any kind of numeric character in any script.
For use in Latin-based scripts, when non-English languages are needed, like Spanish, for instance: éstas, apuntó; will in the latter become; stas and apunt. The former also works on non-Latin based languages.
For all Indo-European Languages, add p{Mn} for Arabic and Hebrew vowels:

s.replaceAll("^[^\\p{L}^\\p{N}^\\p{Mn}\\s%]+|[^\\p{L}^\\p{N}^\\p{Mn}\\s%]+$", "")

For Dravidian languages, the vowels may surround the consonant - as opposed to Semitic languages where they are "within" the character - like ಾ. For this use p{Me} instead. For all languages use:

s.replaceAll("^[^\\p{L}^\\p{N}^\\p{M}\\s%]+|[^\\p{L}^\\p{N}^\\p{M}\\s%]+$", "")

See regex tutorial for a list of Unicode categories

like image 29
Danielson Avatar answered Dec 28 '22 05:12

Danielson