简体   繁体   English

如何在与正则表达式模式不匹配的字符串中查找字符

[英]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? 然后将字符替换为“”。在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] 查找(?!(?![|=;])[\\p{L}\\p{N}\\p{M}\\p{P}\\p{Zs}])[\\S\\s]
Replace nothing nothing

https://regex101.com/r/Sn3DuL/1 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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