简体   繁体   English

使用java中的正则表达式验证用户输入字符串以匹配特定模式

[英]Validate a user input string to match specific pattern using regex in java

I need to validate a user input string to match specific pattern using regex in java.我需要在 java 中使用正则表达式验证用户输入字符串以匹配特定模式。 user input needs to match the following syntax: sv32i-a- where "sv" is always mandatory followed by 32 or 64, then "i" or "c" then "-" then "a" or "b" then "-" and then " " an empty space and then a possible repetition on the string like (sv32i-a- sv64c-b- ).用户输入需要匹配以下语法: sv32i-a- 其中“sv”始终是强制性的,后跟 32 或 64,然后是“i”或“c”,然后是“-”,然后是“a”或“b”,然后是“-”然后是 "" 一个空格,然后可能在字符串上重复 (sv32i-a- sv64c-b- )。 Just getting confused.刚开始糊涂。 Thank you!谢谢!

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- )

So: (?:sv(?:32|64)[ic]-[ab]- )+所以: (?:sv(?:32|64)[ic]-[ab]- )+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM