1

Can somebody please show me how to do a Java regex that takes in a string and returns a string with all characters removed BUT a-z and 0-9?

I.e. given a string a%4aj231*9.+ it will return a4aj2319

thanks.

3 Answers 3

1

\d is digit, \p{L} is a-z and A-Z.

str.replaceAll("[^\\d\\p{L}]", "");
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the quick reply..I just realized, what if I want to preserve spaces as well?
Just add any characters you don't want to replace within the square brackets.
\p{L} also matches a ton of other Unicode characters, such as Δ, ね, and 傻. This expression will leave them all intact.
0
str = str.replaceAll("[^a-z0-9]+", "");

If you also meant to include uppercase characters, then you could use

str = str.replaceAll("[^A-Za-z0-9]+", "");

or the slightly leeter

str = str.replaceAll("[_\\W]+", "");

Comments

0

If you want a-z and 0-9 but not A-Z then

str.replaceAll("[^\\p{Lower}\\p{Digit}]", "");

1 Comment

I didn't go with \w because that includes underscores as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.