Volodymyr Levytskyi
Volodymyr Levytskyi

Reputation: 3432

How to create regex that matches only not spaces and not letters?

I have String but very long.

I need to remove all not white spaces and all not letters :

My pattern is :

String content=readUrl();
content.replaceAll("(\\S)|(^\\[a-z])", "");

And this doesn't work.

Why does my regex replace everything?

Upvotes: 0

Views: 74

Answers (2)

ajb
ajb

Reputation: 31689

Breaking it down:

content.replaceAll("(\\S)|(^\\[a-z])", "");

will replace everything that matches the pattern. The | means a sequence matches the pattern if it matches the part on the left, or it matches the part on the right. The pattern on the left, (\\S), matches every character that isn't white space. So every character that isn't white space will match, and will be eliminated. I think the part on the right will only match if your string actually begins with the five characters [a-z] (since you've told it to treat the [ literally), so it doesn't really have an effect on the result. But the result will be that the result will be just white space.

Upvotes: 0

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can use a negated character class:

(?i)[^\\sa-z]+

or better if you want to support all alphabets:

[^\\s\\p{L}]+

Upvotes: 4

Related Questions