Presentation is loading. Please wait.

Presentation is loading. Please wait.

REGULAR EXPRESSION Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar.

Similar presentations


Presentation on theme: "REGULAR EXPRESSION Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar."— Presentation transcript:

1 REGULAR EXPRESSION Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar to the Perl programming language and very easy to learn. A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. They can be used to search, edit, or manipulate text and data

2 import java.util.regex.*; class RegexExample1 {
In the below example, the regular expression .*book.* is used for searching the occurrence of string “book” in the text. import java.util.regex.*; class RegexExample1 { public static void main(String args[]) String content = "This is Chaitanya " + "from Beginnersbook.com."; String pattern = ".*book.*"; boolean isMatch = Pattern.matches(pattern, content); System.out.println("The text contains 'book'? " + isMatch); } Output: The text contains 'book'? true

3 The java.util.regex package primarily consists of the following three classes
Pattern Class − A Pattern object is a compiled representation of a regular expression. The Pattern class provides no public constructors. To create a pattern, you must first invoke one of its public static compile() methods, which will then return a Pattern object. These methods accept a regular expression as the first argument. Matcher Class − A Matcher object is the engine that interprets the pattern and performs match operations against an input string. Like the Pattern class, Matcher defines no public constructors. You obtain a Matcher object by invoking the matcher() method on a Pattern object. PatternSyntaxException − A PatternSyntaxException object is an unchecked exception that indicates a syntax error in a regular expression pattern.

4 java.util.regex.Pattern class: 1) Pattern.matches()
We have already seen the usage of this method in the above example where we performed the search for string “book” in a given text. This is one of simplest and easiest way of searching a String in a text using Regex. String content = "This is a tutorial Website!"; String patternString = ".*tutorial.*"; boolean isMatch = Pattern.matches(patternString, content); System.out.println("The text contains 'tutorial'? " + isMatch); Limitations: This way we can search a single occurrence of a pattern in a text. For matching multiple occurrences you should use the Pattern.compile() method (discussed in the next section).

5 2)Pattern.compile() 3)Pattern.matcher() method
In the above example we searched a string “tutorial” in the text, that is a case sensitive search, however if you want to do a CASE INSENSITIVE search or want to do search multiple occurrences then you may need to first compile the pattern using Pattern.compile() before searching it in text. This is how this method can be used for this case. String content = "This is a tutorial Website!"; String patternString = ".*tuToRiAl.*"; Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(content); boolean isMatched = matcher.matches(); System.out.println("Is it a Match?" + isMatched); Output: Is it a Match?true

6 java.util.regex.Matcher Class
import java.util.regex.*; class RegexExampleMatcher { public static void main(String args[]) String content = "ZZZ AA PP AA QQQ AAA ZZ"; String string = "AA"; Pattern pattern = Pattern.compile(string); Matcher matcher = pattern.matcher(content); while(matcher.find()) System.out.println("Found at: "+ matcher.start() + " - " + matcher.end()); } Output: Found at: 4 - 6 Found at: Found at:

7 java.util.regex.PatternSyntaxException Example
To see when the PatternSyntaxException is thrown, create a Java class called SimplePatternSyntaxExceptionExample with the following source code: package com.javacodegeeks.examples; import java.util.regex.Pattern; public class SimplePatternSyntaxExceptionExample { public static void main(String... args) String regex = “["; // invalid regex Pattern pattern = Pattern.compile(regex); }

8 Output: Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed character class near index 0 [ ^at java.util.regex.Pattern.error(Unknown Source) at java.util.regex.Pattern.clazz(Unknown Source) at java.util.regex.Pattern.sequence(Unknown Source) at java.util.regex.Pattern.expr(Unknown Source) at java.util.regex.Pattern.compile(Unknown Source) at java.util.regex.Pattern.(Unknown Source) at com.javacodegeeks.examples.SimplePatternSyntaxExceptionExample.main(SimplePatternS yntaxExceptionExample.java:9)


Download ppt "REGULAR EXPRESSION Java provides the java.util.regex package for pattern matching with regular expressions. Java regular expressions are very similar."

Similar presentations


Ads by Google