Java String & Regex Tutorials
Java String methods grouped by task with Java 25 examples, from equals() and compareTo() to strip(), split() and replaceAll(), with pitfalls and FAQs.
In Java, regular expressions (often referred to as regex or regexp) are powerful tools for pattern matching and manipulating text. Java provides built-in support for regular expressions through the java.util.regex package.
We can use regular expressions in Java for tasks such as searching, matching, and replacing text based on specific patterns.
Java String methods grouped by task with Java 25 examples, from equals() and compareTo() to strip(), split() and replaceAll(), with pitfalls and FAQs.
Regex match any character in Java with the dot, .* and .+, plus DOTALL and [\s\S] for line breaks, lazy .*?, literal dots and character classes.
Regex end of line and start of line in Java with $ and ^, MULTILINE mode, the trailing line break trap, \A and \z, and Windows line breaks with \R.
Remove non-ASCII and non-printable characters in Java with \P{ASCII}, \p{Cntrl} and \p{C}, keep line breaks, and turn accents into ASCII with Normalizer.
Credit card regex for Java with Visa, Mastercard 2-series, Amex, Discover, Diners and JCB, brand detection with named groups and a Luhn check.
A UK postcode regex for Java that accepts all six Royal Mail formats, a strict variant, input normalization, named groups and the gov.uk regex bug.
Canadian postal code regex for Java that follows the Canada Post letter rules, accepts lowercase and unspaced input, and reads the province.
Validate a US ZIP code regex in Java for 5-digit and ZIP+4 formats, normalize user input, find ZIP codes in text and avoid storing them as numbers.
ISBN regex Java guide for ISBN-10 and ISBN-13, with check digit code, a complete validator, a formatted regex and ISBN-10 to ISBN-13 conversion.
SSN regex for Java that rejects the numbers the SSA never assigns, accepts spaces or no dashes with a backreference, and masks SSNs in logs.
Use a Java regex to limit the number of lines in text with \R and \V, set a minimum and maximum, handle Windows line breaks and text blocks.
Limit the number of words in Java with a bounded regex group, stop slow backtracking with a word boundary, and count words with Unicode-aware rules.
Check regex min and max length in Java with {min,max} and anchors, limit characters too, handle line breaks, lookaheads, emoji and the {,n} trap.
The regex for alphanumeric characters is ^[a-zA-Z0-9]+$. Java examples for length limits, letter plus digit rules, spaces, Unicode letters and cleanup.
Java date format validation with a regex for the shape and LocalDate with ResolverStyle.STRICT for real dates, with ISO, US and EU formats and traps.
International phone number regex for Java in E.164, E.123 and EPP formats, input normalization, and full validation with libphonenumber.
Validate a US phone number with a Java regex that follows NANP rules and balanced parentheses, then format it with groups or store it as E.164.
Java email validation regex compared from simple to strict, with the HTML5 browser rule, RFC length limits, IDN domains and confirmation emails.
Match Greek characters in a Java regex with \p{IsGreek} for the script or \p{InGreek} and \p{InGreekExtended} for blocks, plus accents and case.
Currency symbol regex for Java with \p{Sc}. Find dollar, euro and rupee signs, extract prices and avoid the two dollar sign traps in Java 25.
Match the trademark symbol U+2122 in a Java regex with \u2122, capture brand names, handle (R) and (C) signs, and convert (TM) and HTML entities.
Match an exact word in Java with \bword\b and find(), find words that contain text, match any or all words, ignore case and quote user input safely.
Learn how the regex word boundary \b works in Java, find words that start or end with given letters, match whole words and avoid common mistakes.
Write a regex for misspelled words in Java with character classes, optional letters and alternation, then find and fix every spelling in one pass.
Build a Java password validation regex with lookaheads, show which rule failed, reject user names safely, and compare it with NIST SP 800-63B-4 rules.
Java regex metacharacters with examples for each one, how to escape them in a Java string, and the split() and replaceAll() bugs they cause.
HowToDoInJava provides tutorials and how-to guides on Java and related technologies.
It also shares the best practices, algorithms & solutions and frequently asked interview questions.