Regex to replace all leading characters not a-Z

java, regex

Solution

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.

Problem

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_]+", "") ```

Original source