How to give range in lookhead using regex e.g ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$
How to give range in lookhead using regex e.g ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$
Question-
1]String length is 2 to 5
2]String contains at least 1 char and maximum 3 char
3]Atleast one number
I want do using lookhead.
What i tried but not working
^(?=(.[a-z]){1,3})(?=.[0-9]).{2,5}$
Sorry, that was me being practically blind... ;-)
– Yunnosch
13 mins ago
Might you have an example input set?
– CertainPerformance
5 mins ago
1 Answer
1
Try using below regex
^(?=([A-Za-z0-9]{1,3}$))(?=(.*[0-9].*)).*
^(?=([A-Za-z0-9]{1,3}$))(?=(.*[0-9].*)).*
I have two look-ahead
1) Ensure that it should have 3 characters
2) It should at least have 1 number
Sample Code
public static void main(String args) {
String regex = "^(?=([A-Za-z0-9]{1,3}$))(?=(.*[0-9].*)).*";
Pattern pattern = Pattern.compile(regex);
System.out.println(pattern.matcher("AB2").find());
}
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.
^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$
– user3890872
16 mins ago