I have a Java regex:
^[a-zA-Z_][a-zA-Z0-9_]{1,126}$
It means:
- Begin with an alphabetic character or underscore character.
- Subsequent characters may include letters, digits or underscores.
- Be between 1 and 127 characters in length.
Now, I want to replace a string having characters not in that regex with a underscore.
Example:
final String label = "23_fgh99@#";
System.out.println(label.replaceAll("^[^a-zA-Z_][^a-zA-Z0-9_]{1,126}$", "_"));
But the result is still 23_fgh99@#.
How can I "convert" it to _3_fgh99__?
2, not[a-zA-Z_], no matches.label.replaceAll("^[^a-zA-Z_]|(?<!^)[^a-zA-Z0-9_]", "_"). It outputs_3_fgh99__, though.