StackStalk
  • Home
  • Java
    • Java Collection
    • Spring Boot Collection
  • Python
    • Python Collection
  • C++
    • C++ Collection
    • Progamming Problems
    • Algorithms
    • Data Structures
    • Design Patterns
  • General
    • Tips and Tricks

Saturday, May 10, 2014

Using Regular Expression in Java

 May 10, 2014     Java     No comments   

This article provides an introduction to usage of regular expressions in Java with some examples. Regular Expression or Regex in the computing terms refers to a sequence of symbols and characters expressing a string or pattern to be searched within a longer piece of text. There is a different syntax to regular expressions beyond the Java programming language. It is essential to understand the basics of regular expressions clearly to understand or create complex regular expressions. Regular Expression syntax in Java is more similar to the one used with Perl language.

Regex Constructs

In general regular expression include characters, character classes and quantifiers. The table below provides a reference to most common Regex constructs taken from the Java reference material.
Construct
Matches
Characters
x The character x
\\ The backslash character
\t The tab character
\n The newline character
\r The carriage-return character
Character Classes
[abc] a, b, or c (simple class)
[^abc] Any character except a, b, or c (negation)
[a-zA-Z] a through z or A through Z, inclusive (range)
Predefined character classes
. Any character (may or may not match line terminators)
\d A digit: [0-9]
\s A whitespace character: [ \t\n\x0B\f\r]
\S A non-whitespace character: [^\s]
\w A word character: [a-zA-Z_0-9]
\W A non-word character: [^\w]
Boundary Matchers
^ The beginning of a line
$ The end of a line
\b A word boundary
\B A non-word boundary
Quantifiers
X? X, once or not at all
X* X, zero or more times
X+ X, one or more times
X{n} X, exactly n times
X{n,} X, at least n times
X{n,m} X, at least n but not more than m times

Regex Java APIs

Java supports java.util.regex for regular expressions processing. 
  1. Primarily there are 2 classes Pattern and Matcher.
  2. Pattern is used to define a regular expression. Use the Pattern.compile() factory method to create a pattern.
  3. Matcher is used to match the pattern against a input sequence. Use Pattern.matcher() to get create a Matcher.
Following example shows how to use the Pattern and Matcher.
package com.sourcetricks.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExTest {
 
 public static void main(String[] args) {

  // Matches the entire region
  String str = "java";
  Pattern pattern = Pattern.compile("java");
  Matcher matcher = pattern.matcher(str);
  if ( matcher.matches() ) {
   System.out.println("Pattern found.");
  }
  
  // Find if a pattern exists
  str = "This is a java tutorial";
  pattern = Pattern.compile("java");
  matcher = pattern.matcher(str);
  if ( matcher.find() ){
   System.out.println("Pattern found at location = " + matcher.start());
  }
  
  // Using flags to perform case insensitive match
  str = "This is a Java tutorial";
  pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
  matcher = pattern.matcher(str);
  if ( matcher.find() ){
   System.out.println("Pattern found at location = " + matcher.start());
  }
  
  // Split a string using a whitespace
  str = "This is a Java tutorial";
  pattern = Pattern.compile("\\s");
  String[] words = pattern.split(str);
  for ( String word : words )
   System.out.println(word);
  
  // Find all occurrences of a pattern
  str = "This is a Java tutorial. Java is programming language.";
  pattern = Pattern.compile("[jJ]ava");
  matcher = pattern.matcher(str);
  while ( matcher.find() ) {
   System.out.println("Pattern found at location = " + matcher.start());
  }
  // Replace all occurrences of a pattern
  String str1 = matcher.replaceAll("C++");
  System.out.println(str1);
 }
}
This example produces the following output.
Pattern found.
Pattern found at location = 10
Pattern found at location = 10
This
is
a
Java
tutorial
Pattern found at location = 10
Pattern found at location = 25
This is a C++ tutorial. C++ is programming language.
  • Share This:  
Newer Post Older Post Home

0 comments:

Post a Comment

Follow @StackStalk
Get new posts by email:
Powered by follow.it

Popular Posts

  • Avro Producer and Consumer with Python using Confluent Kafka
    In this article, we will understand Avro a popular data serialization format in streaming data applications and develop a simple Avro Produc...
  • Monitor Spring Boot App with Micrometer and Prometheus
    Modern distributed applications typically have multiple microservices working together. Ability to monitor and manage aspects like health, m...
  • Server-Sent Events with Spring WebFlux
    In this article we will review the concepts of server-sent events and work on an example using WebFlux. Before getting into this article it ...
  • Implement caching in a Spring Boot microservice using Redis
    In this article we will explore how to use Redis as a data cache for a Spring Boot microservice using PostgreSQL as the database. Idea is to...
  • Python FastAPI microservice with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Spring Boot with Okta and OPA
    Authentication (AuthN) and Authorization (AuthZ) is a common challenge when developing microservices. In this article, we will explore how t...
  • Getting started with Kafka in Python
    This article will provide an overview of Kafka and how to get started with Kafka in Python with a simple example. What is Kafka? ...
  • Getting started in GraphQL with Spring Boot
    In this article we will explore basic concepts on GraphQL and look at how to develop a microservice in Spring Boot with GraphQL support. ...

Copyright © StackStalk