There is a regex and I need to find the character not matching the regex. Then replace the character with "" in. How to achieve this in JAVA?
Pattern : ^((?![\|\=\;])[\p{L}\p{N}\p{M}\p{P}\p{Zs}])+$
Sample Text: HAIRCUT $42 PER PERSON
Required output: HAIRCUT 42 PER PERSON
Just negate what you already have.
Find (?!(?![|=;])[\\p{L}\\p{N}\\p{M}\\p{P}\\p{Zs}])[\\S\\s]
Replace nothing
https://regex101.com/r/Sn3DuL/1
(?!
(?! [|=;] )
[\p{L}\p{N}\p{M}\p{P}\p{Zs}]
)
[\S\s]
You can replace the character matching the regex.
String myString = "HAIRCUT $42 PER PERSON";
myString = myString.replaceAll("^((?![\|\=\;])[\p{L}\p{N}\p{M}\p{P}\p{Zs}])+$", "");
Result:
HAIRCUT 42 PER PERSON
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.