Regular Expressions in Java: Theory, Syntax, and Practical Examples
This article provides a comprehensive overview of Java regular expressions, covering their historical background, core syntax such as character literals, character classes, quantifiers, anchors, and logical operators, and demonstrates practical use cases like validation, splitting, and replacement of strings.
Regular Expressions
1. Theory
Regular expressions are used in virtually every development project for validation, splitting, and replacement; they are simple, fast, and often more concise than ordinary string methods.
2. History and Memory
Regex support was added to Java in version 1.4, originally originating from Unix/Linux tools. In Java it is provided by the java.util.regex package, which mainly contains the Pattern and Matcher classes. Most everyday tasks only require a handful of common constructs, which are listed below.
1. Characters (single‑character literals)
Example: a matches the literal character "a" (the same applies to any other literal character).
2. Character Ranges (single‑character classes)
[abc] matches any one of the characters a, b, or c. [^abc] matches any character except a, b, or c. [a-zA-Z] matches any alphabetic character, while [^a-zA-Z] matches any non‑alphabetic character.
3. Shorthand Character Classes
. matches any single character (escaped as \. in a pattern). \d matches a digit (equivalent to [0-9] ). \D matches a non‑digit. \w matches a word character (letters, digits, underscore). \W matches a non‑word character. \s matches whitespace (including space, tab, newline). \S matches any non‑whitespace character.
4. Anchors (mostly used in JavaScript)
^ asserts the start of the input, $ asserts the end of the input.
5. Quantifiers
? – the preceding element may appear 0 or 1 time. + – the preceding element must appear 1 or more times. * – the preceding element may appear 0, 1, or many times. {n} – exactly n occurrences. {n,m} – between n and m occurrences inclusive.
6. Logical Operators
Concatenation: pattern1pattern2 – pattern2 must follow pattern1. Alternation: pattern1|pattern2 – either pattern1 or pattern2. Grouping: (...) – treat the enclosed pattern as a single unit.
3. String Support for Regex (Examples)
Java’s String class provides methods such as matches() , replaceAll() , and split() that internally use the regex engine. The article shows several concrete examples:
Replacing non‑alphabetic characters.
Splitting a string by digits.
Validating a numeric string with a decimal point.
Matching dates using SimpleDateFormat requirements.
Matching phone numbers with optional area code and separator.
Validating email addresses – a simple version (letters, digits, underscore) and a full version with length limits, allowed symbols, and domain suffix restrictions.
Each example includes the regular expression pattern, the Java code snippet (shown as images in the original source), and the expected output (true/false or transformed string).
4. Direct Use of java.util.regex
Although most tasks can be performed with String methods, the article briefly mentions the lower‑level API:
Creating a Pattern object with Pattern.compile() .
Obtaining a Matcher via pattern.matcher(input) .
Using Matcher.find() , Matcher.replaceAll() , etc., for more complex operations such as capturing groups.
The conclusion emphasizes that regular expressions provide powerful string validation and manipulation capabilities across languages; mastering the common symbols is essential for efficient development.
Conclusion
Regular expressions offer a robust way to validate and transform strings; the essential symbols are language‑independent, even though the API usage differs between languages such as Java.
Follow FunTester for more testing tips
140 Interview Questions (UI, Linux, MySQL, API, Security)
Fiddler Learning Pack
How to Operate Redis in JMeter
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.