3

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();
}
Mustofa Rizwan
  • 10,215
  • 2
  • 28
  • 43
sam_k
  • 5,983
  • 14
  • 76
  • 110
  • 1
    "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" Isn't this a bit contradictory that you want 0 only at odd positions and that 0 can be placed at both odd and even? – Hyperboreus Dec 10 '12 at 05:39
  • are you really bent upon using regex here? I think simple loop and checking should be much easier here. – vishal_aim Dec 10 '12 at 05:41
  • 1
    @Shreya: Wouldn't that also match "022222222"? – Hyperboreus Dec 10 '12 at 05:57
  • @Hyperboreus Sorry my bad. i want 0 at even and odd both and 1-6 only on even places. – sam_k Dec 10 '12 at 06:24
  • http://stackoverflow.com/questions/691519/regular-expression-to-match-only-odd-or-even-number This would help lot to achieve the logic you trying to build – Dinesh Prajapati Dec 10 '12 at 05:53

1 Answers1

7

Haven't tried this out, but might work:

(0[0-6])*0?
Hyperboreus
  • 31,997
  • 9
  • 47
  • 87