java - 在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]- )+
推荐阅读
- php - Firefox 中的横向方向
- rabbitmq - 如何一次只处理一条给定类型的消息?
- windows - 如何通过ansible远程复制文件到远程windows服务器
- android - 加载片段的最佳实践(加载部分)
- python - Python 表格图(Datatyp 的限制长度)
- shell - 如何将输出重定向到多个变量?
- mysql - MariaDB:操作太慢了。在过去 30 秒内传输少于 1 字节/秒
- java - 使用 Java 传输文件
- c++ - 这个 C++ 指针使用线程安全吗?
- angular - Angular 6 提供的不起作用-> StaticInjectorError