DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Mastering Persistence: Why the Persistence Layer Is Crucial for Modern Java Applications
  • Exploring Seamless Integration: Jakarta Data and Jakarta Persistence in Jakarta EE 11 With Open Liberty
  • How To Approach Java, Databases, and SQL [Video]
  • Boosting Application Performance With MicroStream and Redis Integration

Trending

  • OneStream Fast Data Extracts APIs
  • Securing Your Applications With Spring Security
  • Monkey-Patching in Java
  • Five Free AI Tools for Programmers to 10X Their Productivity
  1. DZone
  2. Data Engineering
  3. Databases
  4. Backreferences in Java Regular Expressions

Backreferences in Java Regular Expressions

Java backreferences are really important. Learn what they are and how to use them.

Ryan Wang user avatar by
Ryan Wang
·
Sep. 02, 13 · Tutorial
Like (0)
Save
Tweet
Share
12.40K Views

Join the DZone community and get the full member experience.

Join For Free

Backreferences in Java Regular Expressions is another important feature provided by Java.

To understand backreferences, we need to understand group first. Group in regular expression means treating multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses – ”()”. Each set of parentheses corresponds to a group.

Backreferences are convenient, because it allows us to repeat a pattern without writing it again. We can just refer to the previous defined group by using \#(# is the group number). This will make more sense after you read the following two examples.

Example 1: Finding Repeated Pattern

(\d\d\d)\1 matches 123123, but does not match 123456 in a row. This indicates that the referred pattern needs to be exactly the name.

String str = "123456";
Pattern p = Pattern.compile("(\\d\\d\\d)\\1");
Matcher m = p.matcher(str);
System.out.println(m.groupCount());
while (m.find()) {
String word = m.group();
System.out.println(word + " " + m.start() + " " + m.end());
}

1
123123 0 6

Example 2: Finding Duplicate Words

String pattern = "\\b(\\w+)\\b[\\w\\W]*\\b\\1\\b";
Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
String phrase = "unique is not duplicate but unique, Duplicate is duplicate.";
Matcher m = p.matcher(phrase);
while (m.find()) {
String val = m.group();
System.out.println("Matching subsequence is \"" + val + "\"");
System.out.println("Duplicate word: " + m.group(1) + "\n");
}

Matching subsequence is “unique is not duplicate but unique”
Duplicate word: unique

Matching subsequence is “Duplicate is duplicate”
Duplicate word: Duplicate

Note: This is not a good method to use regular expression to find duplicate words. From the example above, the first “duplicate” is not matched.

Why Use Backreferences?

Check out more regular expression examples.

Java (programming language) Database

Published at DZone with permission of Ryan Wang. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Persistence: Why the Persistence Layer Is Crucial for Modern Java Applications
  • Exploring Seamless Integration: Jakarta Data and Jakarta Persistence in Jakarta EE 11 With Open Liberty
  • How To Approach Java, Databases, and SQL [Video]
  • Boosting Application Performance With MicroStream and Redis Integration

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: