如何编写正则表达式以从String中删除所有字母字符

时间:2022-08-27 17:20:21

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

如何从字符串中删除所有字母字符在java / android中使用正则表达式?

val = val.replaceAll("/A/z","");

3 个解决方案

#1


1  

Try this:

尝试这个:

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

Also have a look here:

还看看这里:

Replace all characters not in range (Java String)

替换不在范围内的所有字符(Java String)

#2


1  

This will remove all alphabetical characters

这将删除所有字母字符

    String text = "gdgddfgdfh123.0114cc";
    String numOnly = text.replaceAll("\\p{Alpha}","");

#3


1  

Have a look into Unicode properites:

看看Unicode本身:

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

\ p {L}来自任何语言的任何类型的信件

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}

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

\ p {M}打算与另一个角色组合的角色(例如重音符号,变音符号,封闭框等)

Then you end here:

然后你到此结束:

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

#1


1  

Try this:

尝试这个:

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

Also have a look here:

还看看这里:

Replace all characters not in range (Java String)

替换不在范围内的所有字符(Java String)

#2


1  

This will remove all alphabetical characters

这将删除所有字母字符

    String text = "gdgddfgdfh123.0114cc";
    String numOnly = text.replaceAll("\\p{Alpha}","");

#3


1  

Have a look into Unicode properites:

看看Unicode本身:

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

\ p {L}来自任何语言的任何类型的信件

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}

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

\ p {M}打算与另一个角色组合的角色(例如重音符号,变音符号,封闭框等)

Then you end here:

然后你到此结束:

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