Hey I need to bypass the validation for substring whiteList in the string str but the rest of the string needs to be validated for the special characters '<', '>', '$', '#', '@'.
Assuming that substring is not repeated anywhere else in the main string.
This is the main string
String str = "blah blah blah X has paid $8,894 to the shop owner blah blah blah"
This is the sub string
"String whiteList = "x has paid $8,894 to the shop owner"
;
current validation for the main string
boolean specialChar = Pattern.compile("<|>|\\$|#|@", Pattern.CASE_INSENSITIVE).matcher(str).find();
The substring whiteList contains $ which should not be validated and should return false.
I am looking for a solution using regular expression if its possible. Any help would be greatly appreciated. Thanks~
CodePudding user response:
I do not know about java. Just curious because of the regex involvement.
What about this using this regex:
[^\s<>$#@] \shas paid [\d,] to the shop owner
- It will only catch the required part from the main string
- The variable part in this match is the word before
has
for which we can use^
to avoid such chars presence - Input like
blah blah blah ><$ has paid 8,894 to the shop owner blah blah blah
will be no match for the substring.
P.S. sorry if this is not relevant.
CodePudding user response:
Just go around it:
String stringToCheck;
int pos = str.indexOf(whiteList);
if (pos >= 0) {
stringToCheck =
str.substring(0, pos) str.substring(pos whiteList.length());
} else {
stringToCheck = str;
}
boolean specialChar =
Pattern.compile("[<>$#@]").matcher(stringToCheck).find();