Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do validate String using regular expression in java

Here i want to make regx for string which contains only 0 to 6 numbers only. This String contains 0 to 6 numbers like that.

 Example-1 : "010002030405" Valid String

This String contains only o to 6 numbers so here i used this regx "[0-6]*". But one more thing i want to validate in this string, I want 0 only at odd positions 1-6 will not be on odd positions never. 0 can be place on odd and even both but 1-6 will be place only even positions.

Here i given u some valid and invalid string examples

Valid : 000102000004
invalid : 0023015006

Here i used this code Please suggest me or tell me what i have to change in my regx to satisfy below validation

1) String contains only 0-6 numbers nothing else.
2) 1-6 would be only even positions only they would not be at odd position ever, 0 would be odd and even position.

Code :

public boolean isOptions(String input) {
    String patternString = "[0-6]*";
    Pattern pattern = Pattern.compile(patternString);
    return pattern.matcher(input).matches();
}
like image 871
sam_k Avatar asked Dec 06 '25 04:12

sam_k


1 Answers

Haven't tried this out, but might work:

(0[0-6])*0?
like image 140
Hyperboreus Avatar answered Dec 07 '25 18:12

Hyperboreus