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
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 +. If you have no match, there is nothing that needs replacing. It's actually cleaner.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With