Reputation: 3514
I want to make a regex that must have atlease one number and one alphabet.
onlyText
will not math. But onlyText123
matches.
Upvotes: 0
Views: 700
Reputation: 124225
You can try something like this
String p= "\\w*([a-zA-Z]\\d|\\d[a-zA-Z])\\w*";
System.out.println("1a".matches(p));//true
System.out.println("a1".matches(p));//true
System.out.println("1".matches(p));//false
System.out.println("a".matches(p));//false
([a-zA-Z]\\d|\\d[a-zA-Z])
== letter then number OR number then letter
and before and after it can (but don't have to) be letters and digits (\\w
)
Upvotes: 0
Reputation: 14089
Here you go
^(?=.*[a-zA-Z])(?=.*[\d]).*$
The key is to use a technique called lookaround
Upvotes: 3