Regular Expression Search Program

Regular Expression: Regular Expression is used to perform many string related operations. This is the way to manipulate different kind of operations like search, edit and manipulating string can be performed by the regular expression. It has both type cha

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Regular Expression Search Program

     

Regular Expression: Regular Expression is used to perform many string related operations. This is the way to manipulate different kind of operations like search, edit and manipulating string can be performed by the regular expression. It has both type characters (literal characters and meta characters). In this section, you will learn how to search a string from the given string exactly.

Program Result: Following program gives you the search facility in which it takes two string/text. One is searched in another one text or string and tells you for the occurrence of the given string for searching into a long string. If the string does not lies in the given string from which the string has to be searched.

Code Description

Pattern:
This is the class of the java.util.regex package which is the compiled representation. Specified string is first compiled into an instance of this class. The pattern to be used to create a matcher object which finds the character sequences for the regular expression.

Matcher:
This is also a class of java.util.regex package which is used to match character sequences.

matcher.find():
Above method finds the matched string in the given string for searching. This method returns a boolean value either true or false.

Here is the code of the program:

import java.util.regex.*;
import java.io.*;

public class SearchProgram{
  public static void main(String[] argsthrows IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter string in which we have to search: ");
  String string = in.readLine();
  System.out.print("Enter string to search: ");
  String str = in.readLine();
  Pattern pattern = Pattern.compile(str);
  Matcher matcher = pattern.matcher(string);
  int a = 0;
  while(matcher.find()){
  a = a + 1;
  }
  if(a == 0)
  System.out.println("Word not found.");
  else
  System.out.println("Occurrence of the word is: " + a);
  }
}

Download this example.

Output of the program:

C:\java\util>java SearchProgram
Enter string in which we have to search: dtwerb wewrvwervwe chandan
Enter string to search: w
Occurrence of the word is: 5

C:\java\util>
_

Advertisements

Ads
Share on Google+Share on Google+

Regular Expression Search Program

Posted on: February 22, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Regular Expression Search Program   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
balaji
October 7, 2011
expression

how to convert the expression in to postfix expression in java

Ads

 

Ads