package regEx;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReGex {
public static void main(String[] args) {
String[] s = {"asd", "123", "123abc", "@#$", "ASSF"};
Pattern p = Pattern.compile("^[A-Z]*[a-z]*[0-9]*+$");
for (String s1 : s) {
Matcher m = p.matcher(s1);
if (m.find()) {
System.out.println(s1);
}
}
}
}
Asked
Active
Viewed 77 times
-2

Youcef LAIDANI
- 55,661
- 15
- 90
- 140

Mano Chandran
- 31
- 5
-
3And ... Any problems? – Scary Wombat Sep 20 '17 at 07:14
-
1"^[A-Z]*[a-z]*[0-9]*+$" should be "^[A-Za-z0-9]+$". I recommend regex101 or similar sites to test your regexes. – Fildor Sep 20 '17 at 07:15
-
https://stackoverflow.com/questions/11241690/regex-for-checking-if-a-string-is-strictly-alphanumeric Try Above – Ram Singh Sep 20 '17 at 07:17
-
not working. @Fildor – Mano Chandran Sep 20 '17 at 07:26
-
not enough info @ManoChandran Please read [ask] and improve question. – Fildor Sep 20 '17 at 07:27
-
You have to change the title to **Pick string which contains both alphabets and numbers from the following strings** – Youcef LAIDANI Sep 20 '17 at 07:30
1 Answers
2
Why not just s1.matches("[A-Za-z0-9]+")
instead :
String[] s = {"asd", "123", "123abc", "@#$", "ASSF"};
for (String s1 : s) {
if (s1.matches("[A-Za-z0-9]+")) {
System.out.println(s1);
}
}
Your regex ^[A-Z]*[a-z]*[0-9]*+$
not mean to match alphanumeric strings, it means :
^
start of string- should start with zero or more Upper Letter
[A-Z]*
- followed by zero or more Lower Letter
[a-z]*
- followed by zero or more degits
[0-9]*
+
i don't see why this$
end of string
Edit
I think you mean the input should contains both alphabets and numbers in this case you can use this regex instead (?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)
:
if (s1.matches("(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)")) {

Youcef LAIDANI
- 55,661
- 15
- 90
- 140
-
-
-
1@ManoChandran According to your question title, this is the desired output. – Fildor Sep 20 '17 at 07:18
-
-
@ManoChandran in this case you can use `(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)` check my edit – Youcef LAIDANI Sep 20 '17 at 07:26