JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Program: Java regex with case insensitive.


Description:

We need to pass Pattern.CASE_INSENSITIVE constant to Pattern.compile() method, along with regular expression. This makes case insensitive matches.


Code:
package com.java2novice.regex;

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

public class MyPatternMatchCaseInsensitive {

	public boolean isPatternMatching(String line){
		
		Pattern ptn = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
		Matcher mtch = ptn.matcher(line);
		if(mtch.find()){
			return true;
		}
		return false;
	}
	
	public static void main(String a[]){
		MyPatternMatchCaseInsensitive msp = new MyPatternMatchCaseInsensitive();
		System.out.println(msp.isPatternMatching("This line contains java, lets see"));
		System.out.println(msp.isPatternMatching("Here JAVA is in capital leters, lets see"));
		System.out.println(msp.isPatternMatching("Here C++ is in capital leters, lets see"));
	}
}

Output:
true
true
false
<< Previous Program | Next Program >>

List Of Regular Expression Sample Programs:

  1. Simple regex pattern matching using string matches().
  2. Simple java regex using Pattern and Matcher classes.
  3. Java regex with case insensitive.
  4. How to validate IP address using regular expression?
  5. How to replace a pattern using regular expression in java?
  6. How to remove multiple spaces with a single space with in a string?
  7. How to validate user name using regular expression?
  8. How to validate email address using regular expression?
  9. How to validate password using regular expression?
  10. How to validate file extensions using regular expression in java?
  11. How to validate date format using regular expression in java?
  12. How to capture or extract a value(s) from text using regular expression in java?
  13. How to split a string using regular expression?
Java2Novice - YouTube Channel
  1. Python Tutorial for Beginners
Knowledge Centre
Different types of Access Modifiers
public: Any thing declared as public can be accessed from anywhere.

private: Any thing declared as private can't be seen outside of its class.

protected: Any thing declared as protected can be accessed by classes in the same package and subclasses in the other packages.

default modifier: Can be accessed only to classes in the same package.
Famous Quotations
Insanity: doing the same thing over and over again and expecting different results.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2019 by Nataraja Gootooru. All Rights Reserved.