The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to use compile and matcher methods of Pattern class | Java Regex

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
How to use compile and matcher methods of Pattern class | Java Regex Posted: Nov 15, 2017 10:48 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: How to use compile and matcher methods of Pattern class | Java Regex
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=Wr_-tZYcSgk&list=UUhwKlOVR041tngjerWxVccw

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

public class RegexDemo1
{
public static void main(String[] args)
{
/*
* . represents single character
*
* Parameters:
*
* regex - The expression to be compiled
*
* Returns:
*
* the given regular expression compiled into a pattern
*/

Pattern pattern = Pattern.compile(".r");
/*
* Parameters:
*
* input - The character sequence to be matched
*
* Returns:
*
* A new matcher for this pattern
*
*/

Matcher matcher = pattern.matcher("br");
/*
* Returns:true if, and only if, the entire region sequence
* matches this matcher's pattern
*/

boolean result = matcher.matches();
System.out.println(result);
}

}
Output
true
RegexDemo2.java
import java.util.regex.Pattern;

public class RegexDemo2
{
public static void main(String[] args)
{
boolean result=Pattern.compile(".r").matcher("ar").matches();
System.out.println(result);
}

}
Output
true
RegexDemo3.java
import java.util.regex.Pattern;

/**
* boolean java.util.regex.Pattern.matches(String regex, CharSequence
* input)
*
* Compiles the given regular expression and attempts to match the
* given input against it
*/

public class RegexDemo3
{
public static void main(String[] args)
{
/*
* Parameters:
*
* regex: The expression to be compiled
*
* input: The character sequence to be matched
*
* Returns:
*
* whether or not the regular expression matches on
* the input
*/

boolean result = Pattern.matches(".r", "ar");
System.out.println(result);
}

}
Output
true

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/RegexDemo_pattern_matcher.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/RegexDemo_pattern_matcher

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/d5af812b33e9f57a10441740b1f80e2caf705d0c/BasicJava/RegexDemo_pattern_matcher/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Read: How to use compile and matcher methods of Pattern class | Java Regex

    Topic: AJAX with CKEditor in Spring Boot Previous Topic   Next Topic Topic: Java finally block after return statement


    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us