I've been struggling with Java Regex I want my regex to with 2 specific characters and then anything that matches the second group
String regex = "(^[a-zA-Z | _])([a-zA-Z0-9\\-_^\\s]*)";
Pattern pattern = Pattern.compile(regex);
String s1 = "hello world";
String s2 = "_Sau90-jds";
String s3 = "5_idsjd";
String s4 = "A-next";
ArrayList<String> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
for (String string : list) {
Matcher matcher = pattern.matcher(string);
if (matcher.find()) {
System.out.println(matcher.group(0));
}
}
The result I want :
_Sau90-jds
A-suivant
But I keep having
hello world
_Sau90-jds
A-suivant
My string has to start with a letter a-zA-Z or "_" and then it can ONLY contain letters, digits, underscores and hyphens, which means no White spaces.
I tried String regex = "(^[a-zA-Z | _])([a-zA-Z0-9\\-_\\S]*)"
And String regex = "(^[a-zA-Z | _])([a-zA-Z0-9\\-_]*)"
but both of them gave me
hello
_Sau90-jds
A-next