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

sam*_*m_k 3 java regex string validation android

在这里,我想为字符串制作regx,仅包含0到6个数字.此字符串包含0到6个这样的数字.

 Example-1 : "010002030405" Valid String
Run Code Online (Sandbox Code Playgroud)

这个字符串只包含0到6个数字所以我在这里使用了这个regx "[0-6]*".但是我想在这个字符串中验证还有一件事,我想在奇数位置只需要0,而不是奇数位置.0可以放在奇数和偶数两个,但1-6将只放置偶数位置.

在这里,我给了你一些有效和无效的字符串示例

Valid : 000102000004
invalid : 0023015006
Run Code Online (Sandbox Code Playgroud)

在这里,我使用此代码请建议我或告诉我我必须在我的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.
Run Code Online (Sandbox Code Playgroud)

代码:

public boolean isOptions(String input) {
    String patternString = "[0-6]*";
    Pattern pattern = Pattern.compile(patternString);
    return pattern.matcher(input).matches();
}
Run Code Online (Sandbox Code Playgroud)

Hyp*_*eus 7

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

(0[0-6])*0?
Run Code Online (Sandbox Code Playgroud)

  • 因为首先是0然后是0到6之间的任何东西.冲洗并重复(这种2位数的模式).然后可能添加最终0. (3认同)