-1

My regular expression is following:

^([0369]|[258][0369]*[147]|[147]([0369]|[147][0369]*[258])*[258]|[258][0369]*[258]([0369]|[147][0369]*[258])*[258]|[147]([0369]|[147][0369]*[258])*[147][0369]*[147]|[258][0369]*[258]([0369]|[147][0369]*[258])*[147][0369]*[147])*$

This allow us too divisible by 3 numbers but it also allowing me to 0 which is not require me please help me out it restrict me to insert 0 and allow me 1. so finally my requirement is it allow us all multi-player of 3 and we can also enter value 1 .

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ravendra Kumar
  • 1,072
  • 10
  • 29

2 Answers2

1

From what I understand, you need to accept any number that is a multiple of 3, along with the exception of 1.

As suggested by @Florian regex is not the tool of choice here. But his answer is accepting all non-multiples of 3, which is incorrect.

The following simple function will do:

bool isValid(int input)
{
    return ((0 == (input % 3)) || (1 == input));
}
CinCout
  • 9,486
  • 12
  • 49
  • 67
-1

Regex is not what you want here. While possible, it is definitely not the tool you want to be using for a task like that.

Instead, translate this in whatever language you are using

See other answer, my code was wrong!

Should be simple enough, and I am pretty sure % is available in almost ALL languages.

F.P
  • 17,421
  • 34
  • 123
  • 189
  • 1
    Why the extra check for `input == 1`? It'll be covered by the first case itself. – CinCout Aug 22 '17 at 04:42
  • 1
    `1 % 3 == 1` which is greater than zero, so the first condition itself will be `true` and the function returns. Second condition is redundant and never executes. – CinCout Aug 22 '17 at 06:23
  • 1
    `return (input % 3) == 0 || input == 1;` will return `true` for all multiples of 3, and 1. – CinCout Aug 22 '17 at 06:24
  • @CinCout --- Sorry, it's still early in the morning. Don't do bools in the morning, kids. Deleted my comment. --- I rechecked the post (it is quite old) and my answer is obviously false. Feel free to edit it as you wish. – F.P Aug 22 '17 at 06:26
  • Your code is accepting all non-multiples of 3 instead. Added a separate correct answer. – CinCout Aug 22 '17 at 06:36
  • @CinCout Yeah, I noticed that now. No idea why OP hasn't picked up on that. -- I think editing the answer would be better because it's unlikely OP will change the accepted answer after such a long time... – F.P Aug 22 '17 at 06:57