0

Have searched for my solution on google and finally decided to post a question here.

My requirement is to send only A C and R characters as input and the maximum length of the string passed should not be more than 6 characters.

Following are the valid states:

A,C,R,

A,R,C,

R,A,C,

R,C,A,

C,R,A,

C,A,R,

R,C,

C,R,

A,R,

R,C,

A,

......

and so on. All possible combinations of A, C, R, are possible but the maximum lenght should not increase 6 char.

So far I am able to perform pattern validation like ([ACR],)+ but also want to perform size validation in same regex.

So for now, I'm using condition like

if(!status.matches("([ACR],)+") || status.length()>6){
SOP
}

This implementation of size I have to use across multiple methods.

Thanking in advance.

2
  • This is very easily found with google. You need to improve your searching technique. Commented Apr 24, 2012 at 9:28
  • Are the commas considered as chars? Should the string ends with a comma? Commented Apr 24, 2012 at 9:28

2 Answers 2

1

This should work:

([ACR],){1,3}
Sign up to request clarification or add additional context in comments.

Comments

0

This regex will allow a pattern to be repeated between 1 and 3 times (in this case the pattern is a dot, i.e. any character):

.{1,3}

Replace the dot with any regex. In your case ([ACR],)

In other words: replace the + with {1,3}

3 Comments

Replacing the + with {1,6} will allow up to 12 chars, not 6 since the group contains 2 chars
it would be 3 for my case , after executive hit and run , this is a combination I found , that solves my problem ^([ACR],){1,3}$
@GuillaumePolet Yes, it gives the number of times the pattern is allowed to be repeated. Edited answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.