简体   繁体   中英

How to find a character in a string which is not matching the regex pattern

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.

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