Introduction to Regular Expressions
In Java, double backslashes (\\) are used to represent a single backslash (\) in regular expressions. Understanding greedy and non-greedy modes is crucial: - Greedy Mode: Matches the longest possible substring that fits the pattern.
- Non-Greedy Mode: Matches the shortest possible substring that fits the pattern.
Metacharacters
Quantifiers
\\: Escapes special characters in regular expressions.*: Matches zero or more occurrences of the preceding element.+: Matches one or more occurrences of the preceding element.?: Matches zero or one occurrence of the preceding element.{n}: Matches exactly n occurrences of the prceeding element.{n,}: Matches atleast n occurrences of the preceding element.{n,m}: Matches between n and m occurrences of the preceding element.(?i): Makes the match case-insensitive.
Alternation
|: Acts as an OR operator between two patterns.
Grouping and Backreferences
- Capturing Groups:
(...)for unnamed groups and(?<name>...)</name>or(?'name'...)for named groups. - Non-Capturing Groups:
(?:...). - Positive Lookahead:
(?=...). - Negative Lookahead:
(?!...).
Character Classes
[]: Defines a set of characters to match.[^]: Defines a set of characters not to match.-: Defines a range of characters within a character class..: Matches any character except newline.\\d: Matches any digit (equivalent to [0-9]).\\D: Matches any non-digit (equivalent to [^0-9]).\\w: Matches any alphanumeric character and underscore (equivalent to [0-9a-zA-Z_]).\\W: Matches any non-alphanumeric character and underscore (equivalent to [^0-9a-zA-Z_]).\\s: Matches any whitespace character.\\S: Matches any non-whitespace character.
Anchors
^: Asserts position at start of a line.$: Asserts position at end of a line.\\b: Asserts position at a word boundary.\\B: Asserts position at a non-word boundary.
Greedy vs Non-Greedy Matching
Greedy Example in Java
String input = "<p class='active'>zhangSan</p>";
Pattern pattern = Pattern.compile("<p.*>");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group(0)); // <p class='active'>zhangSan</p>
}
Non-Greedy Example in Java
String input = "<p class='active'>zhangSan</p>";
Pattern pattern = Pattern.compile("<p.*?>");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group(0)); // <p class='active'>
}
Global Flags in JavaScript
g: Global search.i: Case-insensitive search.
Global Flag Example in JavaScript
let text = 'from 2025-07-01 to 2025-07-03';
let regex = /\d{4}-\d{1,2}-\d{1,2}/g;
let match = regex.exec(text);
console.log(match[0]); // 2025-07-01
match = regex.exec(text);
console.log(match[0]); // 2025-07-03
Case-Insensitive Global Search Example in JavaScript
let text = 'Tom is tom';
let regex = /tom/ig;
let match = regex.exec(text);
console.log(match[0]); // Tom
match = regex.exec(text);
console.log(match[0]); // tom
Application: Date Parsing Tool in Java
public static Date convertToDate(String date) throws ParseException {
String format = null;
Locale locale = null;
if (date.matches("^\\d{4}-\\d{2}-\\d{2}$")) {
format = "yyyy-MM-dd";
} else if (date.matches("^\\d{4}/\\d{2}/\\d{2}$")) {
format = "yyyy/MM/dd";
} else if (date.matches("^\\d{4}\\.\\d{2}\\.\\d{2}$")) {
format = "yyyy.MM.dd";
} else if (date.matches("^\\d{4}\\d{2}\\d{2}$")) {
format = "yyyyMMdd";
} else if (date.matches("^\\d{4}年\\d{2}月\\d{2}日$")) {
format = "yyyy年MM月dd日";
} else if (date.matches("^\\d{4}-\\d{1,2}-\\d{1,2} \\d{2}:\\d{2}:\\d{2}$")) {
format = "yyyy-MM-dd HH:mm:ss";
} else if (date.matches("^[A-Z][a-z]{2} [A-Z][a-z]{2} \\d{2} \\d{2}:\\d{2}:\\d{2} CST \\d{4}$")) {
format = "EEE MMM dd HH:mm:ss 'CST' yyyy";
locale = Locale.US;
} else if (date.matches("^[A-Z][a-z]{2} [A-Z][a-z]{2} \\d{2} \\d{2}:\\d{2}:\\d{2} GMT\\+08:00 \\d{4}$")) {
format = "EEE MMM dd HH:mm:ss z yyyy";
locale = Locale.ENGLISH;
}
SimpleDateFormat sdf = (locale == null) ? new SimpleDateFormat(format) : new SimpleDateFormat(format, locale);
return sdf.parse(date);
}