[英]Regex to replace all leading characters not a-Z
I need to replace all non-letter characters that appear at the start before any letter for example 我需要替换在任何字母之前出现在开头的所有非字母字符
$ %5hello w8r^ld becomes hello w8r^ld
$%5hello w8r ^ ld成为你好w8r ^ ld
This regex I got now works greate for replacing none word characters but does not replace numbers 我现在得到的这个正则表达式可以用来替换任何单词字符,但不会替换数字
s.replaceFirst("^[\\W_]+", "")
You are using the wrong character class. 您正在使用错误的字符类。 Use
使用
s.replaceFirst("^[^a-zA-Z]+", "")
That is 那是
^ 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
Following comments from @anubhava, I changed the *
to a +
. 根据@anubhava的评论,我将
*
更改为+
。 If you have no match, there is nothing that needs replacing. 如果没有匹配,则无需更换。 It's actually cleaner.
它实际上更清洁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.