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]", "");
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]+$", "");
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With