[英]java regex: match input starting with non-number or empty string followed by specific pattern
[英]Validate a user input string to match specific pattern using regex in java
我需要在 java 中使用正则表达式验证用户输入字符串以匹配特定模式。 用户输入需要匹配以下语法: sv32i-a- 其中“sv”始终是强制性的,后跟 32 或 64,然后是“i”或“c”,然后是“-”,然后是“a”或“b”,然后是“-”然后是 "" 一个空格,然后可能在字符串上重复 (sv32i-a- sv64c-b- )。 刚开始糊涂。 谢谢!
public class StringValidation {
static boolean result = true;
//Help needed here.
static String syntax = "^rv\\d{2}$"; //Code goes here but not sure about the syntax..
public static boolean isTrue(String stringToValidate) {
result = stringToValidate.matches(syntax);
return result;
}
}
sv here "sv" is always mandatory
(?:32|64) followed by 32 or 64,
[ic] then "i" or "c"
- then "-"
[ab] then "a" or "b"
- then "-"
and then " " an empty space
(?:xxx)+ and then a possible repetition on the string like (sv32i-a- sv64c-b- )
所以: (?:sv(?:32|64)[ic]-[ab]- )+
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.