Reputation: 391
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=
Upvotes: 0
Views: 47
Reputation: 7286
This works:
^Eval:((([^=;]+)=([^=;]*));?)+(?<!;)$
It's basically the same as your attempt, but with a negative look-behind for ;
at the end to block the trailing semicolon.
Eval:
([^=;]+)=
([^=;]*)
((([^=;]+)=([^=;]*));?)+
(?<!;)$
Upvotes: 1