简体   繁体   中英

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. 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- ). 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]- )+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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