Regex for finding between 1 and 3 character in a string

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Regex for finding between 1 and 3 character in a string



I am trying to write a regex which should return true, if [A-Za-z] is occured between 1 and 3, but I am not able to do this


public static void main(String args) {
String regex = "(?:([A-Za-z]*){3}).*";
String regex1 = "(?=((([A-Za-z]){1}){1,3})).*";

Pattern pattern = Pattern.compile(regex);
System.out.println(pattern.matcher("AD1CDD").find());
}



Note: for consecutive 3 characters I am able to write it, but what I want to achieve is the occurrence should be between 1 and 3 only for the entire string. If there are 4 characters, it should return false. I have used look-ahead to achieve this





Could you add some inputs and outputs for better understanding.
– JavaFan
3 mins ago





Try ^[^A-Za-z]*[A-Za-z](?!(?:[^A-Za-z]*[A-Za-z]){3}). See live demo here regex101.com/r/a0eZx2/1
– revo
2 mins ago




^[^A-Za-z]*[A-Za-z](?!(?:[^A-Za-z]*[A-Za-z]){3})









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived