简体   繁体   中英

java regular expression negate a character and replace string

String s = "=?ISO-2022-JP?B?GyRCRZi?= =?ISO-2022-JP?B?QDg7OlWiE8?= =?ISO-2022-JP?B?JTkkT=?= =?ISO-2022-JP?B?kjaHaA?=";
String replRegex = "[^=]\\?= =\\?ISO-2022\\-JP\\?B\\?";

stringtoDecode= s.replaceAll(replRegex, "" );

result what I got is

=?ISO-2022-JP?B?GyRCRZQDg7OlWiEJTkkT=?= =?ISO-2022-JP?B?kjaHaA?=

but what I am expecting is

=?ISO-2022-JP?B?GyRCRZiQDg7OlWiE8JTkkT=?= =?ISO-2022-JP?B?kjaHaA?=

the character ?= =?ISO-2022-JP?B? is missing in the result. I want "?= =?ISO-2022-JP?B?" to be replaced with empty string if it is not presided by "=" .

Am I doing anything wrong here? Please suggest

The [^=] part is consuming the character i . You can capture this 'consumed' character and replace it:

String replRegex = "([^=])\\?= =\\?ISO-2022-JP\\?B\\?";
stringtoDecode= s.replaceAll(replRegex, "$1" );

ideone demo .

Note that you don't need to escape hyphens. (You need to escape hyphens only if you mean a literal hyphen in a character class and that the hyphen is somewhere in the middle of that character class.

OutPut:

=?ISO-2022-JP?B?GyRCRZiQDg7OlWiE8JTkkT=?= =?ISO-2022-JP?B?kjaHaA?=

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