简体   繁体   中英

java - String.replaceAll to replace all characters not in pattern

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__ ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM