REGEX
About
Regular Expressions (regex) are patterns used to match sequences of characters. In Spring applications, regex plays an important role across various modules such as:
Web request routing
Data validation
Security rules
Conditional bean creation
Service-layer logic
Regex increases flexibility by allowing dynamic pattern matching rather than relying on hardcoded values.
1. REGEX in Controller URL Mapping
Spring allows the use of regex in path variables to restrict the format of dynamic URL segments. This is useful for building routes that only match specific patterns, such as numeric IDs, usernames, UUIDs, or custom tokens.
Syntax
In @GetMapping, @RequestMapping, or @PostMapping, regex can be added inside {variable:regex}.
Example
@GetMapping("/product/{id:[0-9]+}")
public ResponseEntity<String> getProductById(@PathVariable String id) {
return ResponseEntity.ok("Valid product ID: " + id);
}This endpoint matches
/product/123, but not/product/abc.Regex used:
[0-9]+(one or more digits).
Use Case Scenarios
Enforce numeric-only IDs in URLs
Match slugs (e.g.,
{slug:[a-z0-9-]+})Route based on specific formats like dates or UUIDs
2. REGEX in Bean Validation using @Pattern
@PatternSpring Boot integrates with Jakarta Bean Validation (JSR 380, formerly JSR 303), which includes @Pattern annotation to validate strings using regex. This is typically used in DTOs, forms, and REST request bodies.
Example
This ensures name and email follow the required patterns.
Annotate the controller method with
@Validor@Validated.
Why Use It?
Avoids manual validation logic
Centralizes format rules
Makes validation declarative and reusable
3. REGEX in Spring Security (regexMatchers)
regexMatchers)Spring Security allows us to apply access control based on URL patterns using regex. This provides fine-grained control over access to endpoints.
Example
/admin/1234will be accessible only to users withADMINrole./user/johnwill be accessible only to users withUSERrole.
Why Use It?
More flexible than Ant-style (
/admin/**)Enables dynamic access rules based on format (e.g., only allow numeric IDs)
4. REGEX in Spring Expressions (@ConditionalOnExpression)
@ConditionalOnExpression)Spring Expression Language (SpEL) can use regex for conditional logic, such as loading beans only if a property matches a pattern.
Example
This bean is only loaded when the system property
envis set todevorqa.
Benefits
Enables environment-specific configuration using patterns
Helps in feature toggling or staged deployment logic
5. REGEX in Property Files (External Config)
We can define regex patterns in application.properties or application.yml and inject them for use in validation or pattern matching in services.
Example
application.properties:
Service:
This allows updating validation rules without changing code.
6. REGEX Using java.util.regex.Pattern in Services
java.util.regex.Pattern in ServicesWe can manually use regex for advanced matching in service-layer logic, especially for scenarios not covered by annotations.
Example
This is useful when:
Building custom validation logic
Processing or filtering large text inputs
Integrating regex in business rules
7. REGEX with Apache Commons RegexValidator
RegexValidatorApache Commons Validator provides a reusable, thread-safe way to use regex in our services.
Example
8. REGEX in File Parsing and Text Processing
Regular expressions (regex) are essential for extracting, filtering, transforming, and validating patterns in text. In Spring Boot (or any Java application), when reading files or processing strings, regex provides a powerful and flexible way to work with content dynamically.
Key Use Cases
Searching for specific lines in a file (e.g., error logs)
Extracting structured data (e.g., phone numbers, email addresses)
Replacing or cleaning unwanted characters or formats
Parsing logs, CSVs, and configuration files
Validating structured inputs like dates, IDs, codes
Regex is especially helpful when:
File formats are not standardized (e.g., free text logs)
We need to process large text files line by line
We want to apply complex pattern-based rules
1. Filtering Lines that Match a Pattern
We may want to process only lines that contain a specific keyword or pattern from a large file (like logs or reports).
Example – Extract lines containing "ERROR"
.*ERROR.*matches any line containing the word "ERROR".Efficient for scanning and reporting.
2. Extracting Data from a Line (Pattern Matching)
Use regex groups to extract specific parts of a line. Ideal for logs, delimited text, or loosely structured content.
Example – Extract timestamp and level from a log line
Log line:
This approach is excellent for structured log analysis or custom reporting tools.
3. Replacing or Cleaning Text
Regex is often used to sanitize data by removing unwanted characters, trimming patterns, or replacing formats.
Example – Remove all special characters from a string
Example – Replace all multiple spaces with a single space
Useful for normalizing inconsistent inputs.
4. Validating Each Line in a File
Apply validation rules to each line (e.g., is it a valid email, phone number, or ID).
Example – Check if all lines are valid email addresses
This technique helps validate file-based input in batch jobs, ETL processes, or user uploads.
5. Extracting All Occurrences from a Single Line
Use find() method to match multiple occurrences of a pattern in the same string.
Example – Extract all numbers from a sentence
Use this in analytics, reporting, or transformation scenarios.
6. Parsing Delimited Files with Optional Validation
While CSVs can be parsed with libraries (like OpenCSV), regex helps when we have inconsistent or semi-structured files.
Example – Parse and validate simple CSV manually
7. Detecting Sensitive Information in Logs
Regex can be used to detect and redact sensitive fields such as passwords, keys, tokens, or credit card numbers before logging.
Example – Mask password field in a string
This is useful for log filtering and compliance with security standards.
8. Custom Regex Utility Method
Example – Generic reusable utility
Usage:
Last updated