Java Program to find two letter capital words using Regex

Java Program to find two letter capital words in Java using Regex:

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

public class RegexAppealAddress {
	public static void main(String[] args) {
		String input = "Javadomain is also called as JD which contains JAVA tutorials and programmings";
		// This prints JD and JA
		// Pattern regexPattern = Pattern.compile("\\s[A-Z]{2,2}");
		// This prints only JD
		Pattern regexPattern = Pattern.compile("\\s[A-Z]{2,2}\\s");
		Matcher matcher = regexPattern.matcher(input);
		while (matcher.find()) {




1. \\s for whitespaces
2. [A-Z] for only capital letters
3. {2,2} for minimum two letters and maximum two letters.

Java Recommended Books:

535 total views, 1 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in