I Wanna check the validity of a string then try to split it using regex.
The regex should group the input by ";" and each group should be a key-value pair,but my regex group the inputs wrong,where is the problem with my regex?
Here is my function that uses regex:
public static boolean verify(String str) {
String pattern = "^(Eval:)+((.+?)=(([^;]*$)))+";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
if(m.matches()){
return true;
}else{
return false;
}
}
Valid Examples:
Eval:tag=val;tag2=
Eval:tag=val;tag2=val2
Eval:tag=
Eval:tag=;tag2=
Invalid Examples:
Eval:tag=;tag2=;
Eval:tag;tag2=;
Eval:tag=tag2=
Eval:tag=;tag2=<-- is this really valid? I would hate to be the person who is working with your data if this be the case.str.matches("^(Eval:)+((.+?)=(([^;]*$)))+")