In PCRE regular expressions

  • \p{P} is "Any punctuation character"
  • \p{Z} is "Any whitespace character"

See the "EXPLANATION" section on the right: https://regex101.com/r/ZFIKpv/1

Answer from Shamus on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 8 )
July 21, 2026 - The following Predefined Character classes and POSIX character classes are in conformance with the recommendation of Annex C: Compatibility Properties of Unicode Regular Expression , when UNICODE_CHARACTER_CLASS flag is specified. Categories that behave like the java.lang.Character boolean ismethodname methods (except for the deprecated ones) are available through the same \p{prop} syntax where the specified property has the name javamethodname.
🌐
W3Schools
w3schools.com › java › java_regex.asp
Java Regular Expressions
Regular expressions can be used to perform all types of text search and text replace operations. Java does not have a built-in Regular Expression class, but we can import the java.util.regex package to work with regular expressions.
🌐
Medium
medium.com › @kaustubh.saha › regular-expressions-in-java-b806152aaa93
Regular Expressions in Java. A regular expression is essentially a… | by Kaustubh Saha | Medium
November 29, 2025 - First character: a letter (A–Z, excluding D, F, I, O, Q, U, W, Z in practice, but regex usually allows all letters). Second character: a digit (0–9). Third character: a letter. Optional space. Fourth character: a digit. Fifth character: a letter. Sixth character: a digit. ... import java.util.regex.Pattern; import java.util.regex.Matcher; public class CanadianPostalCodeValidator { // Regex for Canadian postal code: Letter-Digit-Letter [space optional] Digit-Letter-Digit private static final String POSTAL_CODE_REGEX = "^[A-Za-z]\\d[A-Za-z][ ]?\\d[A-Za-z]\\d$"; private static final Pattern P
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › util › regex › Pattern.html
Pattern (Java Platform SE 7 )
Predefined Character classes and POSIX character classes are in conformance with the recommendation of Annex C: Compatibility Properties of Unicode Regular Expression , when UNICODE_CHARACTER_CLASS flag is specified. Categories that behave like the java.lang.Character boolean ismethodname methods (except for the deprecated ones) are available through the same \p{prop} syntax where the specified property has the name javamethodname.
🌐
GeeksforGeeks
geeksforgeeks.org › java › regular-expressions-in-java
Regular Expressions in Java - GeeksforGeeks
May 22, 2026 - Supports powerful text manipulation operations such as find and replace ... In Java, regular expressions are supported through the java.util.regex package, which mainly consists of the following classes:
🌐
TutorialsPoint
tutorialspoint.com › character-class-p-javamirrored-java-regex
JAVA Character Class \p{javaMirrored}
package com.tutorialspoint; import java.util.regex.Matcher; import java.util.regex.Pattern; public class JavaCharacterClassDemo { private static final String REGEX = "\\p{javaMirrored}"; private static final String INPUT = "{Bb12 \tc!"; public static void main(String[] args) { // create a pattern Pattern pattern = Pattern.compile(REGEX); // get a matcher object Matcher matcher = pattern.matcher(INPUT); while(matcher.find()) { //Prints the start index of the match.
🌐
JRebel
jrebel.com › blog › java-regular-expressions-cheat-sheet
Java Regular Expressions (Regex) Cheat Sheet | JRebel
July 30, 2025 - The Pattern.compile method takes a String, which is the RegEx that defines a set of matching strings. Naturally, it has to have a tricky syntax, otherwise a single string defining the pattern can only represent itself. A regular character in the Java Regex syntax matches that character in the text.
Find elsewhere
🌐
Vogella
vogella.com › tutorials › JavaRegularExpressions › article.html
Regular expressions in Java - Tutorial
This tutorial describes the usage of regular expressions in Java with modern examples and best practices. It covers basic regex syntax, Java’s Pattern and Matcher classes, practical examples for common use cases, and important security considerations.
🌐
DigitalOcean
digitalocean.com › community › tutorials › regular-expression-in-java-regex-example
Regular Expression in Java: Regex Examples & Tutorial | DigitalOcean
August 3, 2022 - Pattern class also contains matches method that takes regex and input String as argument and return boolean result after matching them. So below code works fine for matching input String with a regular expression in Java. String str = "bbb"; System.out.println("Using String matches method: "+str.matches(".bb")); System.out.println("Using Pattern matches method: "+Pattern.matches(".bb", str));
🌐
TutorialsPoint
tutorialspoint.com › character-class-p-javauppercase-java-regex
Character class p{javaUpperCase} Java regex.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main( String args[] ) { //Regular expression to match lower case letters String regex = "^\p{javaUpperCase}+$"; //Getting the input data Scanner sc = new Scanner(System.in); System.out.println("Enter 5 input strings: "); String input[] = new String[5]; for (int i=0; i<5; i++) { input[i] = sc.nextLine(); } //Creating a Pattern object Pattern p = Pattern.compile(regex); System.out.println("Strings with only upper case characters: "); for(int i=0; i<5;i++) { //Creating a Matcher object Matcher m = p.matcher(input[i]); if(m.matches()) { System.out.println(m.group()); } } } } Enter 5 input strings: Raju RAMU rahman radha SUnDar* Strings with only upper case characters: RAMU ·
🌐
Regex101
regex101.com
regex101: build, test, and debug regex
Online regex tester and debugger with real-time match highlighting, detailed explanations, substitutions, unit tests, benchmarking, and code generation. Supports PCRE2, JavaScript, Python, Go, Java, .NET, Rust, POSIX ERE, and POSIX BRE.
🌐
TutorialsPoint
tutorialspoint.com › javaregex › javaregex_posix_class_ascii.htm
Posix Character Class \p{ASCII} Match
package com.tutorialspoint; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PosixCharacterClassDemo { private static final String REGEX = "\\p{ASCII}"; private static final String INPUT = "Bb12\tc"; public static void main(String[] args) { // create a pattern Pattern pattern = Pattern.compile(REGEX); // get a matcher object Matcher matcher = pattern.matcher(INPUT); while(matcher.find()) { //Prints the start index of the match.
🌐
Baeldung
baeldung.com › home › java › core java › a guide to java regular expressions api
A Guide To Java Regular Expressions API | Baeldung
January 8, 2024 - Let’s start with the simplest use case for a regex. As we noted earlier, when we apply a regex to a String, it may match zero or more times. The most basic form of pattern matching supported by the java.util.regex API is the match of a String literal.
🌐
Stack Abuse
stackabuse.com › guide-to-regular-expressions-in-java
Guide to Regular Expressions in Java
October 24, 2023 - In this guide, we're going to take a deep dive into Regular Expressions, how they work and how to use them in Java. We'll mainly be taking a look at the Pattern and Matcher classes of the regex package, followed by some practical examples and common tasks.
🌐
TutorialsPoint
tutorialspoint.com › posix-character-classes-p-print-java-regex
Posix character classes p{Print} Java regex
January 9, 2020 - This class matches all the printable characters. Live Demo · import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class PrintableCharacters { public static void main(String args[]) { //Reading String from user System.out.println("Enter a string"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); //Regular expression String regex = "[\p{Print}]"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex); //Retrieving the matcher object Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count++; } System.out.println("Number of printable characters: "+count); } } Enter a string test Number of printable characters: 4 ·
🌐
CodeGym
codegym.cc › java blog › strings in java › regular expressions in java
Java RegEx Regular expressions | CodeGym
February 19, 2025 - Now that you're getting comfy with regular expressions in Java, let's look at some quick patterns you can use right away. After all, understanding abstract concepts is great, but seeing them in action is even better! Ready? Imagine you want to check if a string contains only alphabetic characters. This is a super common scenario—like validating a username or a country name. Here's how you can do it in Java: import java.util.regex.Pattern; public class RegexLettersOnly { public static void main(String[] args) { String pattern = "^[A-Za-z]+$"; // Only letters, at least 1 character long String testString = "HelloWorld"; boolean matches = Pattern.matches(pattern, testString); System.out.println("Is letters only?
🌐
Tutorialspoint
tutorialspoint.com › java › java_regular_expressions.htm
Java - Regular Expressions
Study methods review the input string and return a Boolean indicating whether or not the pattern is found − · Replacement methods are useful methods for replacing text in an input string − · Following is the example that counts the number of times the word "cat" appears in the input string − · import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { private static final String REGEX = "\\bcat\\b"; private static final String INPUT = "cat cat cat cattie cat"; public static void main( String args[] ) { Pattern p = Pattern.compile(REGEX); Matcher m = p.matcher(INPUT); // get a matcher object int count = 0; while(m.find()) { count++; System.out.println("Match number "+count); System.out.println("start(): "+m.start()); System.out.println("end(): "+m.end()); } } }
🌐
TutorialsPoint
tutorialspoint.com › javaregex › javaregex_unicode_class_substraction.htm
Unicode Character Class [\p{L}&&[^\p{Lu}]]
package com.tutorialspoint; import java.util.regex.Matcher; import java.util.regex.Pattern; public class UnicodeCharacterClassDemo { private static final String REGEX = "[\\p{L}&&[^\\p{Lu}]]"; private static final String INPUT = "!BSab\u03B1"; ...