Thursday 30 November 2017

How to use Regex finder to find a word | Java Regex | Java Regular Expressions | Regex in java


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

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

/**
 * 
 * Java Regex Finder Example
 *
 */

public class RegexDemo
{
    public static void main(String[] args)
    {
        try (Scanner sc = new Scanner(System.in))
        {
            while (true)
            {
                System.out.println("Enter regex pattern:");
                String regexPattern = sc.nextLine();                
                Pattern pattern = Pattern.compile(regexPattern);
                Matcher matcher = pattern.matcher("Welcome to india peter");
                boolean found = false;
                while (matcher.find())
                {
                    System.out.println("I found the text "
                            + matcher.group() + " starting at index "
                            + matcher.start()
                            + " and ending at index "
                            + matcher.end());
                    found = true;
                }
                if (!found)
                {
                    System.out.println("No match found.");
                }
            }
        }

    }

}
Output
Enter regex pattern:
india
I found the text india starting at index 11 and ending at index 16
Enter regex pattern:
peter
I found the text peter starting at index 17 and ending at index 22

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

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/950c9f162cb72194a90be45ab9293fca2b84f41c/BasicJava/RegexDemo_finder/?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
  • No comments:

    Post a Comment