• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Java Regular Expressions

July 26, 2017 By j2eereference Leave a Comment

Java regular expressions are unique series of characters, which assists in matching or locating other strings or collection of strings, with a unique syntax applied in a pattern. Java offers a regex package for matching with regular expressions.

The java.util.regex package mainly is composed of 3 classes.

  1. Pattern Class

A Pattern element is a compiled form of a regular expression. Pattern class does not offer public constructors. It is used for defining a pattern for the regex mechanism. The following compile methods permit a regular expression as the primary argument.

  • static Pattern compile(String regex) piles up the specified regex and sends the instance of pattern as response.
  • Matcher matcher(CharSequence input) builds a matcher that equals the specified input to the pattern.
  • static boolean matches(String regex, CharSequence input) acts as the blend of compile and matcher methods.
  • String[] split(CharSequence input) divides the input string between matches of specified pattern.
  • String pattern() delivers the regex pattern.

 

  1. Matcher Class

A Matcher element understands the pattern and then executes matching actions against an input string. Matcher class does not offer public constructors. It executes Match Result interface. The matcher methods are:

  • boolean matches() checks if the regular expression equals the pattern.
  • boolean find() locates the subsequent expression that equals the pattern.
  • boolean find(int start) locates the upcoming expression that equals the pattern from the specified start number.
  • String group() delivers the equated subsequence.
  • int start() delivers the first index of the equated subsequence.
  • int end() delivers the final index of the equated subsequence.
  • int groupCount() delivers the total of the equated subsequence.

 

  1. PatternSyntaxException

A PatternSyntaxException element is an unverified exception that points out syntax mistakes in a regular expression pattern. The class methods are:

  • public String getDescription() fetches the report of the mistakes.
  • public int getIndex() fetches the fault index.
  • public String getPattern() fetches the invalid regular expressions.
  • public String getMessage() delivers a multi-line string that contains (i) the report of the error and index, (ii) the invalid pattern, and (iii) a visual sign of the fault index inside the pattern.

Java Regular Expressions Example

In the below example, the regular expressions “.x”, “.x.”, and “..x” are used for finding the occurrence of the string in the given words. Here, a dot signifies a single character. The regular expressions can be written in 3 approaches.

 

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class RGEXP {
public static void main(String args[]) {
 
// Way-1
Pattern s = Pattern.compile(".x");
Matcher a = s.matcher("ox");
boolean y = a.matches();
 
// Way-2
boolean y2 = Pattern.compile(".x.").matcher("oxe").matches();
 
// Way-3
boolean y3 = Pattern.matches("..x", "xx");
 
System.out.println("Regular expression testing " + y + " " + y2 + " " + y3);
}
}

Output for the above program would be

Regular expression testing true true false

In the above example,
• The 1st approach is true as 2nd character is x.
• The 2nd approach is true as 2nd character is x.
• The 3rd approach output is false as it requests for 3rd character and “xx” has only 2 characters.

Related Posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Difference between Stack and Heap memory
  • What is ThreadMXBean in java?
  • What is serialVersionUID
  • What is exchanger in concurrency?
  • UncaughtExceptionHandler in java

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.