Java Regular Expressions

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.

Related Tags

Tutorials

Regex – Match Any Character(s)

In regex, we can match any character using period “.” character. To match only a given set of characters, we should use character classes.

Regex – Match Start or End of String (Line Anchors)

Regular expressions, commonly referred to as regex, are powerful tools for pattern matching and manipulation of text. The regex patterns enable developers to perform intricate string searches, substitutions, and validations. Among the plethora of regex features, line anchors are the key components for precisely identifying the beginning or end of …

Java Regex – Credit Card Number Validation

In this java regular expression tutorial, we will learn to use regex to validate credit card numbers. We will learn about number format and validations of credit card numbers from multiple providers such as VISA, Mastercard, Amex and Diners etc. 1. Valid Credit Card Numbers Formats On an actual credit …

Regex for UK Postal Code Validation

Learn to use regular expressions for UK postcode validation in Java. You can modify the regex to suit it for any other format such as US postal codes.

Java Regex to Validate ISBN

Learn to use regular expressions to test whether a user has entered a valid International Standard Book Number (ISBN).

Regex to Validate SSN (Social Security Number)

In this Java regex tutorial, we will learn to use regular expressions to test whether a user has entered a valid Social Security number in your application or website form. 1. Valid SSN Pattern United States Social Security numbers are nine-digit numbers in the format AAA-GG-SSSS with the following rules. …

Regex to Limit the Number of Lines in Text

In this Java regex tutorial, we will learn to test whether several lines in input text are between some minimum and maximum limit, without regard for how many total characters appear in the string. The regex for matching the number of lines will depend on the exact characters or character …

Java Regex to Limit Number of Words

In this Java regex tutorial, we will learn to test whether the number of words in input text is within a certain minimum and maximum limit.

Regex for Alphanumeric Characters (+ Example)

To create a regular expression that allows only alphanumeric characters, we can use the regex pattern ^[a-zA-Z0-9]+$. This pattern ensures that the string consists solely of uppercase alphabets, lowercase alphabets, and digits. Alphanumeric characters are all alphabets and numbers i.e. letters A–Z, a–z, and digits 0–9. 1. Alphanumeric Regex Pattern …

Java date validation using RegEx

In this Java date validation using regex, we will learn to validate simple date formats such as mm/dd/yy, mm/dd/yyyy, dd/mm/yy and dd/mm/yyyy. Here we want to use a regex that simply checks whether the input is a valid date without trying to eliminate things such as February 31st. We might …

Regex to Validate International Phone Numbers

In this regex tutorial, we will learn to validate international phone numbers based on industry-standard notation specified by ITU-T E.123. The rules and conventions used to print international phone numbers vary significantly around the world, so it’s hard to provide meaningful validation for an international phone number unless you adopt …

Regex for North American Phone Number Validation

In this regex tutorial, we will learn to validate user-entered phone numbers for a specific format (in this example phone numbers are formatted in North American format) and if the numbers are correct then reformat them to a standard format for display. I have tested formats including 1234567890, 123-456-7890, 123.456.7890, …

Java Email Validation using Regex

Simple Java email validation example. Learn to validate email in Java using regular expressions. Five different regex patterns for email validation.

Regex: Matching Exact Word or Contains Word

Java regex word boundary example to match specific word or check if string contain word using Java regular expressions. java regex contain word example. java regex match specific word example.

Java Regex Password Validation Example

Password validation is the need of almost all applications today. There are various ways to validate passwords from writing everything manually to using third-party available APIs. In this password validation tutorial, we are building a password validator using regular expressions. 1. Regex for Validating the Passwords ((?=.*[a-z])(?=.*d)(?=.*[@#$%])(?=.*[A-Z]).{6,16}) The above regular …

Regex Meta Characters (with Examples)

A regular expression defines a search pattern for strings. This pattern may match one or several times or not at all for a given string. The abbreviation for regular expression is regex. Regular expressions can be used to search, edit, and manipulate text. Regex meta characters are special symbols that …

About Us

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.