gpt4 book ai didi

java - 如何使用正则表达式给出查找头的范围,例如 ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$

转载 作者:行者123 更新时间:2023-12-02 10:57:55 25 4
gpt4 key购买 nike

Question-
1]String length is 2 to 5
2]String contains at least 1 char and maximum 3 char
3]Atleast one number

我想使用lookhead。我尝试过但不起作用^(?=(.[a-z]){1,3})(?=.[0-9]).{2,5}$

最佳答案

尝试使用下面的正则表达式

(?=([\\w\\W]{2,5}$))(?=(.*([0-9]){1,}.*))^[^ A-Za-z]*[A-Za-z](?!(?:[^A-Za-z]*[A-Za-z]){3})

public static void main(String[] args) {
String regex = "(?=([\\w\\W]{2,5}$))(?=(.*([0-9]){1,}.*))^[^A-Za-z]*[A-Za-z](?!(?:[^A-Za-z]*[A-Za-z]){3}).*";

Pattern pattern = Pattern.compile(regex);
System.out.println(pattern.matcher("AB2").find()); // output true
System.out.println(pattern.matcher("AB2C").find()); // output true
}

关于java - 如何使用正则表达式给出查找头的范围,例如 ^(?=(.*[a-z]){1,3})(?=.*[0-9]).{2,5}$,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51577465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com