Advanced String Filtering
Complex String Processing Techniques
Advanced string filtering goes beyond basic pattern matching, enabling sophisticated text manipulation and validation strategies.
Lookahead and Lookbehind Assertions
Positive Lookahead
Matches pattern only if followed by specific pattern
Pattern p = Pattern.compile("\\w+(?=@labex\\.io)");
Matcher m = p.matcher("[email protected] [email protected]");
while (m.find()) {
System.out.println(m.group()); // Prints usernames
}
Negative Lookahead
Matches pattern not followed by specific pattern
Pattern p = Pattern.compile("\\d+(?!px)");
Matcher m = p.matcher("100px 200 300px");
while (m.find()) {
System.out.println(m.group()); // Prints 200
}
Filtering Workflow
graph TD
A[Input String] --> B[Regex Pattern]
B --> C{Advanced Filtering}
C -->|Lookahead| D[Conditional Matching]
C -->|Replacement| E[Text Transformation]
C -->|Splitting| F[String Segmentation]
Advanced Filtering Techniques
1. Complex Replacements
Replace patterns with sophisticated logic
String input = "Price: $45.99, Discount: 20%";
String filtered = input.replaceAll(
"\\$(\\d+\\.\\d+)",
match -> {
double price = Double.parseDouble(match.group(1));
return String.format("$%.2f", price * 0.9);
}
);
2. Conditional Filtering
List<String> emails = Arrays.asList(
"[email protected]",
"[email protected]",
"[email protected]"
);
List<String> filteredEmails = emails.stream()
.filter(email -> email.matches(".*@labex\\.io"))
.collect(Collectors.toList());
Advanced Filtering Strategies
Strategy |
Description |
Use Case |
Lookahead |
Conditional matching |
Validation with context |
Negative Matching |
Exclude specific patterns |
Data cleaning |
Transformation |
Complex replacements |
Text normalization |
- Compile patterns once
- Use non-capturing groups
- Minimize backtracking
- Leverage stream operations
Real-world Filtering Scenarios
Log File Processing
String logPattern = "(?<timestamp>\\d{4}-\\d{2}-\\d{2}) " +
"(?<level>ERROR|WARN) " +
"(?<message>.*)";
Pattern p = Pattern.compile(logPattern);
Data Validation
String passwordRegex = "^(?=.*[A-Z])" + // At least one uppercase
"(?=.*[a-z])" + // At least one lowercase
"(?=.*\\d)" + // At least one digit
".{8,}$"; // Minimum 8 characters
Pro Tips from LabEx
- Understand regex complexity
- Test patterns incrementally
- Use online regex visualization tools
- Consider performance implications
Mastering advanced string filtering empowers developers to handle complex text processing challenges efficiently!