Home Regularexpressions Match string using regular expression without Case sensitivity
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Match string using regular expression without Case sensitivity

Advertisement
This Example describes the way to match the String using regular expression. For this we are going to make program named Matching_Casesensitive.java.

Match string using regular expression without Case sensitivity

     

This Example describes the way to match the String using regular expression. For this we are going to make program named Matching_Casesensitive.java. The steps involved in program Matching_Casesensitive.java are described below:-

String regex="^java":-Defining regression as string. Here (^java) means all the words which are named as java .

Pattern.CASE_INSENSITIVE| Pattern.MULTILINE:-This enables the pattern to match case insensitive. The word can either be in caps or not.

Matching_Casesensitive.java

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

public class Matching_Casesensitive {

  public static void main(String[] args) {
 
  String regex="^java";
 Pattern pattern = Pattern.compile(regex,
  Pattern.CASE_INSENSITIVE| Pattern.MULTILINE);
 String text="java has classes\n" 
 +"Java has methods\n"
  "JAVA have regular expressions\n"
  "Regular expressions are in Java";
 System.out.println("Words from the Text is:-");
  System.out.println(text);
  System.out.println("========================");
 Matcher m = pattern.matcher(text);
  
 System.out.println("Finded words from the Text is:-");
 while (m.find())
  System.out.println(m.group());
  }}

Output of the program:-

Words from the Text is:-
java has classes
Java has methods
JAVA have regular expressions
Regular expressions are in Java
========================
Finded words from the Text is:-
java
Java
JAVA


Download Source Code

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 30, 2008

Ask Questions?    Discuss: Match string using regular expression without Case sensitivity  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com