简体   繁体   English

如何使用java中的正则表达式验证String

[英]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. 在这里,我想为字符串制作regx,仅包含0到6个数字。 This String contains 0 to 6 numbers like that. 此字符串包含0到6个这样的数字。

 Example-1 : "010002030405" Valid String

This String contains only o to 6 numbers so here i used this regx "[0-6]*" . 这个字符串只包含0到6个数字,所以在这里我使用了这个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,而不是奇数位置。 0 can be place on odd and even both but 1-6 will be place only even positions. 0可以放在奇数和偶数两个,但1-6将只放置偶数位置。

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 在这里,我使用此代码请建议我或告诉我我必须在我的regx中更改以满足以下验证

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();
}

没有试过这个,但可能有效:

(0[0-6])*0?

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

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