Skip to main content
The RegularExpression class provides a comprehensive set of predefined regular expression patterns for validating common data formats. These patterns can be used with the regExp validation rule or with the Validators.regExp() static method.

Import

import io.github.ApamateSoft.validator.utils.RegularExpression;

Basic Patterns

NUMBER

Matches strings containing only numeric digits.
RegularExpression.NUMBER = "^\\d+$"
Matches: 123, 456789, 0
Does not match: -123, 12.34, abc
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.NUMBER, "Must contain only digits")
    .build();

validator.isValid("123");    // Valid
validator.isValid("-123");   // Invalid

ALPHABET

Matches strings containing only English letters (case-insensitive).
RegularExpression.ALPHABET = "^[a-zA-Z]+$"
Matches: abc, XYZ, AbCd
Does not match: abc123, hello world, josé

ALPHA_NUMERIC

Matches strings containing only letters and numbers (no spaces or special characters).
RegularExpression.ALPHA_NUMERIC = "^[a-zA-Z0-9]+$"
Matches: abc123, Test01, USER42
Does not match: user_123, test-01, hello world
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.ALPHA_NUMERIC, "Must contain only letters and numbers")
    .build();

validator.isValid("User123");    // Valid
validator.isValid("User_123");   // Invalid

DECIMAL

Matches numeric values including integers, decimals, and negative numbers.
RegularExpression.DECIMAL = "^\\-?\\d*\\.?\\d+?$"
Matches: 123, -123, 123.45, -123.45, .5, 0.99
Does not match: abc, 12.34.56, --123
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.DECIMAL, "Must be a valid number")
    .build();

validator.isValid("19.99");    // Valid
validator.isValid("-5.50");    // Valid
validator.isValid("12..34");   // Invalid

Email and Names

EMAIL

Matches valid email addresses according to RFC standards.
RegularExpression.EMAIL = "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$"
Matches: [email protected], [email protected], [email protected]
Does not match: invalid@, @domain.com, user@domain
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.EMAIL, "Must be a valid email address")
    .build();

validator.isValid("[email protected]");  // Valid
validator.isValid("invalid@");          // Invalid

NAME

Matches proper names with optional spaces (English letters only).
RegularExpression.NAME = "^[a-zA-Z]+(\\s?[a-zA-Z])*$"
Matches: Jesus, Maria Jose, JOHN, Mary Ann
Does not match: José, 123John, Mary Ann (double space)
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.NAME, "Must be a valid name")
    .build();

validator.isValid("John Doe");     // Valid
validator.isValid("John123");      // Invalid

NAME_ES

Matches Spanish names with accented characters and ñ.
RegularExpression.NAME_ES = "^[a-zA-ZáéíóúñÁÉÍÓÚÑ]+(\\s?[a-zA-ZáéíóúñÁÉÍÓÚÑ])*$"
Matches: José, María Pérez, Señor Muñoz
Does not match: José123, María Pérez (double space)
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.NAME_ES, "Debe ser un nombre válido")
    .build();

validator.isValid("José María");   // Valid
validator.isValid("José123");      // Invalid

URL Patterns

Matches any type of URL (www, http, or https).
RegularExpression.LINK = "^((https://)|(www.)|(http://))+([a-zA-Z0-9@:%._+~#=]{2,63})+\\.([a-z]{2,6}\\b)+(.)*$"
Matches:
  • www.google.com
  • http://example.com
  • https://github.com/user/repo
  • http://example.com/api/auth?name=Jesus
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.LINK, "Must be a valid URL")
    .build();

validator.isValid("https://example.com");  // Valid
validator.isValid("not-a-url");            // Invalid
Matches URLs starting with www.
RegularExpression.WWW_LINK = "^www.([a-zA-Z0-9@:%._+~#=]{2,63})+\\.([a-z]{2,6}\\b)+(.)*$"
Matches: www.google.com, www.example.com/path
Does not match: http://google.com, example.com
Matches URLs starting with http://
RegularExpression.HTTP_LINK = "^http://.([a-zA-Z0-9@:%._+~#=]{2,63})+\\.([a-z]{2,6}\\b)+(.)*$"
Matches: http://google.com, http://example.com/api
Does not match: https://google.com, www.google.com
Matches URLs starting with https://
RegularExpression.HTTPS_LINK = "^https://.([a-zA-Z0-9@:%._+~#=]{2,63})+\\.([a-z]{2,6}\\b)+(.)*$"
Matches: https://google.com, https://secure.example.com
Does not match: http://google.com, www.google.com

IP Address Patterns

IP

Matches both IPv4 and IPv6 addresses.
RegularExpression.IP // Complex pattern supporting both IPv4 and IPv6
Matches:
  • IPv4: 127.0.0.1, 192.168.0.109, 10.0.0.1
  • IPv6: ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff, ffff::, ffff::ffff
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.IP, "Must be a valid IP address")
    .build();

validator.isValid("192.168.1.1");                          // Valid
validator.isValid("2001:0db8:85a3:0000:0000:8a2e:0370:7334"); // Valid
validator.isValid("256.1.1.1");                             // Invalid

IPV4

Matches only IPv4 addresses.
RegularExpression.IPV4 = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
Matches: 127.0.0.1, 192.168.0.109, 10.0.0.1, 255.255.255.255
Does not match: 256.1.1.1, 192.168.1, ffff::

IPV6

Matches only IPv6 addresses.
RegularExpression.IPV6 // Complex pattern for full IPv6 validation
Matches:
  • ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
  • ffff::
  • ffff::ffff
  • 2001:0db8:85a3::8a2e:0370:7334

Time Patterns

TIME

Matches time in both 12-hour and 24-hour formats.
RegularExpression.TIME = "^(^(([0-1]?\\d)|(2[0-3])):([0-5][0-9])(:?[0-5][0-9])?$)|(^((0?[1-9])|(1[0-2])):([0-5][0-9])(:[0-5][0-9])?\\s?([aA]|[pP])[mM]$)$"
Matches:
  • 12-hour: 12:59 am, 1:00 pm, 01:00AM, 01:00PM
  • 24-hour: 00:00, 13:00, 23:59
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.TIME, "Must be a valid time")
    .build();

validator.isValid("14:30");      // Valid
validator.isValid("2:30 PM");    // Valid
validator.isValid("25:00");      // Invalid

TIME12

Matches time in 12-hour format with AM/PM.
RegularExpression.TIME12 = "^((0?[1-9])|(1[0-2])):([0-5][0-9])(:[0-5][0-9])?\\s?([aA]|[pP])[mM]$"
Matches: 12:59 am, 1:00 pm, 01:00AM, 11:30 PM
Does not match: 13:00, 24:00, 12:60 am
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.TIME12, "Must be in 12-hour format (e.g., 2:30 PM)")
    .build();

validator.isValid("2:30 PM");    // Valid
validator.isValid("14:30");      // Invalid

TIME24

Matches time in 24-hour format.
RegularExpression.TIME24 = "^(([0-1]?\\d)|(2[0-3])):([0-5][0-9])(:?[0-5][0-9])?$"
Matches: 00:00, 13:00, 23:59, 9:30, 09:30:45
Does not match: 24:00, 13:60, 2:30 PM
Example:
Validator validator = new Validator.Builder()
    .regExp(RegularExpression.TIME24, "Must be in 24-hour format (e.g., 14:30)")
    .build();

validator.isValid("14:30");      // Valid
validator.isValid("2:30 PM");    // Invalid

Additional Patterns

The RegularExpression class also includes these additional patterns:
  • ALPHABET_UPPERCASE - Uppercase letters only
  • ALPHABET_LOWERCASE - Lowercase letters only
  • ALPHABET_ES - Spanish letters with accents
  • ALPHA_NUMERIC_ES - Spanish alphanumeric
  • NAME_LOWERCASE - Lowercase names only
  • NAME_UPPERCASE - Uppercase names only
  • NAME_CAPITALIZE - Capitalized names (e.g., “John Doe”)
  • NAME_LOWERCASE_ES - Spanish lowercase names
  • NAME_UPPERCASE_ES - Spanish uppercase names
  • NAME_CAPITALIZE_ES - Spanish capitalized names
  • INTEGER - Integer numbers (positive or negative)
  • SPECIAL_CHARACTERS - Special character validation
  • PHONE - Phone number patterns
  • CARD - Credit card number patterns

Using with Validators

Regular expressions can be used in two ways:

With Validation Rules

Validator emailValidator = new Validator()
    .required("Email is required")
    .regExp(RegularExpression.EMAIL, "Must be a valid email address");

try {
    emailValidator.isValid("email", "[email protected]");
} catch (InvalidEvaluationException e) {
    System.out.println(e.getMessage());
}

With Static Methods

import io.github.ApamateSoft.validator.utils.Validators;
import io.github.ApamateSoft.validator.utils.RegularExpression;

boolean isValidEmail = Validators.regExp("[email protected]", RegularExpression.EMAIL);
boolean isValidTime = Validators.regExp("14:30", RegularExpression.TIME24);

Custom Regular Expressions

You can also use custom regex patterns with the regExp rule:
// Custom pattern for a specific format
String customPattern = "^[A-Z]{3}-\\d{4}$"; // Matches: ABC-1234

Validator validator = new Validator.Builder()
    .regExp(customPattern, "Must match format: XXX-0000")
    .build();

validator.isValid("ABC-1234");  // Valid
validator.isValid("abc-1234");  // Invalid

Common Use Cases

Form Validation

Validator registrationValidator = new Validator()
    .required("Email is required")
    .regExp(RegularExpression.EMAIL, "Invalid email format")
    .maxLength(100, "Email too long");

API Input Validation

Validator apiKeyValidator = new Validator()
    .required("API key is required")
    .regExp(RegularExpression.ALPHA_NUMERIC, "API key must be alphanumeric")
    .length(32, "API key must be exactly 32 characters");

Time Entry Validation

Validator appointmentTimeValidator = new Validator()
    .required("Time is required")
    .regExp(RegularExpression.TIME12, "Please use 12-hour format (e.g., 2:30 PM)");

URL Validation

Validator websiteValidator = new Validator()
    .required("Website URL is required")
    .regExp(RegularExpression.HTTPS_LINK, "Website must use HTTPS");

Build docs developers (and LLMs) love