簡體   English   中英

如何使用java中的正則表達式驗證String

[英]How to do validate String using regular expression in java

在這里,我想為字符串制作regx,僅包含0到6個數字。 此字符串包含0到6個這樣的數字。

 Example-1 : "010002030405" Valid String

這個字符串只包含0到6個數字,所以在這里我使用了這個regx "[0-6]*" 但是我想在這個字符串中驗證還有一件事,我想在奇數位置只需要0,而不是奇數位置。 0可以放在奇數和偶數兩個,但1-6將只放置偶數位置。

在這里,我給了你一些有效和無效的字符串示例

Valid : 000102000004
invalid : 0023015006

在這里,我使用此代碼請建議我或告訴我我必須在我的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.

代碼:

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