0

How to remove all alphabetical characters from a string usign a regular expression in java/android?

val = val.replaceAll("/A/z","");
Mr.Noob
  • 1,005
  • 3
  • 24
  • 58
  • 4
    Spend 5 minutes reading about [character classes](http://regex.learncodethehardway.org/book/ex4.html) and you'll know. – HamZa Aug 09 '13 at 09:26

3 Answers3

1

Try this:

replaceAll("[a-z]", "");

Also have a look here:

Replace all characters not in range (Java String)

Community
  • 1
  • 1
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
1

Have a look into Unicode properites:

\p{L} any kind of letter from any language

So your regex would look like this

val = val.replaceAll("\\p{L}+","");

To remove also combined letters use a character class and add \p{M}

\p{M} a character intended to be combined with another character (e.g. accents, umlauts, enclosing boxes, etc.)

Then you end here:

val = val.replaceAll("[\\p{L}\\p{M}]+","");
stema
  • 90,351
  • 20
  • 107
  • 135
1

This will remove all alphabetical characters

    String text = "gdgddfgdfh123.0114cc";
    String numOnly = text.replaceAll("\\p{Alpha}","");
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115