我需要替换任何字母之前出现的所有非字母字符,例如
$%5hello w8r ^ ld成为你好w8r ^ ld
我现在得到的这个正则表达式可以用来替换任何单词字符,但不会替换数字
s.replaceFirst("^[\\W_]+", "")
Floris.. 5
您正在使用错误的字符类.使用
s.replaceFirst("^[^a-zA-Z]+", "")
那是
^ start at the beginning of the string [^ ]+ one or more (greedy - keep going until you hit a letter a-zA-Z ascii characters between a-z or A-Z
根据@anubhava的评论,我改为*
a +
.如果没有匹配,则无需更换.它实际上更清洁.
您正在使用错误的字符类.使用
s.replaceFirst("^[^a-zA-Z]+", "")
那是
^ start at the beginning of the string [^ ]+ one or more (greedy - keep going until you hit a letter a-zA-Z ascii characters between a-z or A-Z
根据@anubhava的评论,我改为*
a +
.如果没有匹配,则无需更换.它实际上更清洁.