Tag: regex

[QA] How to use regular expression to find year, month and day of date

String aDate = "date: 04-16-2013";
Pattern datePattern = Pattern.compile("date: (\\d{2})-(\\d{2})-(\\d{4})");
Matcher dateMatcher = datePattern.matcher(aDate);
if (dateMatcher.find()) {
   System.out.println("Month is: " + dateMatcher.group(1));
   System.out.println("Day is: " + dateMatcher.group(2));
   System.out.println("Year is: " + dateMatcher.group(3));
}
Tags : ,

[問題] 如何使用 regular expression 判斷 alphanumeric and underscores 字串

String regex = "^[a-zA-Z0-9_]*$";
String words = "some words ConTain 123_";
 
if (words.matches(regex)) {
// only contains alphanumeric and underscores
}
Tags : ,