Reputation: 2613
I have below statement in Javascript (NodeJS) -
const name = (name) =>
name && !XRegExp('^[\\p{L}\'\\d][ \\p{L}\'\\d-]*[\\p{L}\'-\'\\d]$').test(name)
? 'Invalid' : undefined
This regex is for name can accept .
, -
and (space) and should start with character.
How can I achieve same validation regex in java. I tried below -
@Pattern(regexp = "^(?U)[\\p{L}\\'\\d][ \\p{L}\\'\\d-]*[\\p{L}\\'-\\'\\d]$" ,
message="Invalid name")
String name;
Upvotes: 2
Views: 691
Reputation: 27723
I'm guessing that maybe this expression might work, based on the one you have provided:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^[\\p{L}\\d'][ \\p{L}\\d'-]*[\\p{L}\\d'-]$";
final String string = "éééééé";
final Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
Upvotes: 1