How to use regular expressions in Java to validate strings

Here, I want to make regx for strings that contain only 0 to 6 numbers

Example-1 : "010002030405" Valid String

This string contains only 0 to 6 numbers, so I use the regx "[0-6] *" here But one more thing I want to verify in this string is that I want only 0 in the odd position, not the odd position 0 can be placed in odd and even numbers, but 1-6 will only be placed in even numbers

Here, I give you some examples of valid and invalid strings

Valid : 000102000004
invalid : 0023015006

Here, I use this code. Please advise me or tell me that I must change it in my regx to meet the following 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();
}

Solution

I haven't tried this, but it may work:

(0[0-6])*0?
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>