JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Checkout for Promo Codes

Program: Simple java regex using Pattern and Matcher classes.


Description:

Patter is a compiled representation of a regular expression. Matcher is an engine that performs match operations on a character sequence by interpreting a Pattern.


Code:
package com.java2novice.regex;

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

public class MySimplePatternMatch {

	public boolean isPatternMatching(String line){
	
		Pattern ptn = Pattern.compile("\\d{2}$");
		Matcher mtch = ptn.matcher(line);
		if(mtch.find()){
			return true;
		}
		return false;
	}
	
	public static void main(String a[]){
		MySimplePatternMatch msp = new MySimplePatternMatch();
		System.out.println("Is 'ddfsa12' ends with min 2 digits? "
					+msp.isPatternMatching("ddfsa12"));
		System.out.println("Is 'ddfsa12adf1' ends with min 2 digits? "
					+msp.isPatternMatching("ddfsa12adf1"));
	}
}


Output:
Is 'ddfsa12' ends with min 2 digits? true
Is 'ddfsa12adf1' ends with min 2 digits? 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?
Knowledge Centre
What is race condition?
A race condition is a situation in which two or more threads or processes are reading or writing some shared data, and the final result depends on the timing of how the threads are scheduled. Race conditions can lead to unpredictable results and subtle program bugs. A thread can prevent this from happening by locking an object. When an object is locked by one thread and another thread tries to call a synchronized method on the same object, the second thread will block until the object is unlocked.
Famous Quotations
If you don’t make mistakes, you’re not working on hard enough problems. And that’s a big mistake.
-- Frank Wilczek

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 © 2020 by Nataraja Gootooru. All Rights Reserved.