Reputation: 81
I have a Java regex:
^[a-zA-Z_][a-zA-Z0-9_]{1,126}$
It means:
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__
?
Upvotes: 4
Views: 4526
Reputation: 626903
Use this code:
final String label = "23_fgh99@#";
System.out.println(label.replaceAll("^[^a-zA-Z_]|(?<!^)[^a-zA-Z0-9_]", "_"));
It outputs _3_fgh99__
.
To remove what is "not in the original pattern", you need to negate the first character class and only check a character at the beginning (^[^a-zA-Z_]
), and then check other characters not at the beginning with the negated second character class ((?<!^)[^a-zA-Z0-9_]
). Then, we just use an alternation symbol |
to apply both patterns in 1 replacement operation.
Upvotes: 4