Java Video Tutorial 19

Java Regular ExpressionsIn this part of my Java Video Tutorial, I cover Java Regular Expressions. You use regular expressions when you want to search for data. You then use codes to define what that data looks like.

So, if you are looking for a 5 digit number, you could type \\d{5} and Java does the rest!

The code that follows the video will help you see how easy regular expressions are.

If you like videos like this share it

Code From the Video

import java.util.regex.*;

public class LessonNineteen{
	
	public static void main(String[] args){
		
		String longString = " Derek Banas CA 12345 PA (412)555-1212 johnsmith@hotmail.com 412-555-1234 412 555-1234 "; 
		String strangeString = " 1Z aaa **** *** {{{ {{ { ";
		
		/*
		[ ]  Insert characters that are valid
		[^ ]  Insert characters that are not valid
		\\s  Any white space
		\\S  Any non white space
		{n,m}  Whatever proceeds must occur between n and m times
		*/
		
		// Word must contain letters that are 2 to 20 characters in length
		// [A-Za-z]{2,20} 0r \w{2,20}
		
		regexChecker("\\s[A-Za-z]{2,20}\\s", longString);
		
		/*
		\\d  Any digits 0-9
	 	\\D  Anything not a number
	 	{n}  Whatever proceeds must occur n times
	 	*/
		
		// Only 5 digits
		// \\s[0-9]{5}\\s or \\d{5}
		
		regexChecker("\\s\\d{5}\\s", longString);
		
		/*
		|  Is used for OR clause situations
		*/
		
		// Must start with a A or C, followed by 1 letter in brackets
		// Must be a maximum of 2 characters in length
		// A[KLRZ]|C[AOT]
		
		regexChecker("A[KLRZ]|C[AOT]", longString);
		
		/*
		{n,}  Whatever proceeds must occur at least n times
		+  Whatever proceeds must occur one or more times
		. ^ * + ? { } [ ] \ | ( )  Characters that must be escaped or backslashed
		*/
		
		// Grab any string that contains 1 or more !
		
		regexChecker("(\\{{1,})", strangeString);
		regexChecker("(\\{+)", strangeString);
		
		// Get anything that occurs 3 times except newline
		// .  Anything but newline
		
		regexChecker(".{3}", strangeString);
		
		/*
		\\w  Any word type character A-Z, a-z, 0-9, _
		\\W  Any non word character
		*  Occurs zero or more times
		*/
		
		regexChecker("\\w*", strangeString);
		
		regexChecker("[A-Za-z0-9._\\%-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}", longString);
		
		
		// ?  0 or 1 of what proceeds it
		
		regexChecker("([0-9]( |-)?)?(\\(?[0-9]{3}\\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})", longString);
		
		regexReplace(longString);
		
	}
	
	public static void regexChecker(String theRegex, String str2Check){
		
		// You define your regular expression (REGEX) using Pattern
		
		Pattern checkRegex = Pattern.compile(theRegex);
		
		// Creates a Matcher object that searches the String for
		// anything that matches the REGEX
		
		Matcher regexMatcher = checkRegex.matcher( str2Check );
		
		// Cycle through the positive matches and print them to screen
		// Make sure string isn't empty and trim off any whitespace
		
		while ( regexMatcher.find() ){
			if (regexMatcher.group().length() != 0){
				System.out.println( regexMatcher.group().trim() );
				
				// You can get the starting and ending indexs
				
				System.out.println( "Start Index: " + regexMatcher.start());
				System.out.println( "Start Index: " + regexMatcher.end());
			}
		}
		
		System.out.println();
	}
	
	public static void regexReplace(String str2Replace){
		
		// REGEX that matches 1 or more white space
		
		Pattern replace = Pattern.compile("\\s+");
		
		// This doesn't really apply, but this is how you ignore case
		// Pattern replace = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE);
		
		// trim the string t prepare it for a replace
		
		Matcher regexMatcher = replace.matcher(str2Replace.trim());
		
		// replaceAll replaces all white space with commas
		
		System.out.println(regexMatcher.replaceAll(", "));
		
	}
	
}

25 Responses to “Java Video Tutorial 19”

  1. admin says:

    I got asked how to match any word that starts with a specific character and ends with another character using regular expressions. Here is the answer:

    This will work every time (Even with numbers)

    (?<=\s|^)[Aa]\S+[Bb]\b If you want to eliminate numbers do this (?<=\s|^)[Aa][A-Za-z]+[Bb]\b (?<=\s|^) - The first character is a space or start of string [Aa] - Match for A or a [A-Za-z]+ - Match for 1 or more characters [Bb] - Match for B or b \b - Match for word boundary Question I received We have written to each other before in the past and I visit your Website a lot for information but this is my new questions for concerning regular expressions. I am trying to give a regular expression that would match any string that begins with A and ends with B. Matches would include: AxxxxB AB ABABABAB I am trying this to do this from the terminal in Ubuntu 12.04 LTS. I have spent so much time and watched other youtube videos. None seem to show a way to solve something like this. You have always been helper to my issues in the past so I felt I should reach out to you once more. Thank you for you time and consideration. I hope that helps someone Derek

    • Pralay Biswas says:

      Hi Derek,

      Considering that its a “word”, wont this work?

      “^[aA][a-zA-Z]*[bB]$”

      Thanks,
      Pralay

    • Wilheard says:

      Hi Derek,

      Thank you for all your wonderful videos! I never thought learning JAVA could be so interesting!
      Here I have a question that in
      (?<=\s|^)[Aa]\S+[Bb]\b
      what does the "|^" mean here? I understand that "^" means anything but I cannot figure out in which situation that this regex won't work without "|^" here.

      Thanks

      Wilheard

      • Wilheard says:

        I find the “.” means anything and “^” means the beginning of a string? Now I understand. I’m sorry I was misunderstanding with them.

        Thank you all the same.

        Wilheard

  2. Khalid says:

    Hi Derek,

    I’m sure it’ll come to me but what does putting the ( in a regex do, as in regexChecker(“(\\{{1,})”, strangeString);

    I understand that searching for { it needs to be prefixed by \\.

    Thanks

    Khalid

  3. waheed says:

    sir aoa,
    i start to learn java. i dn’t know how to start.
    please clear that i can start from java or before java learn known about any other language

  4. Kai says:

    Hi Derek:
    Wonderful videos. You are an amazing teacher.
    Just one question. In the 2 to 20 example, why does it not match Banas .. It is a word that is 2 to 20 chars.
    Thanks

    • Derek Banas says:

      Thank you 🙂 The reason it doesn’t grab Banas is because the regular expression is looking for a space before the word. Since the space was eaten up by the previous word it isn’t there. Does that make sense?

  5. Meeran says:

    Hello Derek.. I wonder if you can help me with this..
    I have a text file that contains more than 9000 words and the text is quiet normal.. except the fact that some of the titles are in this way:

    S E N T I N E L S A R E I N T H E S K Y

    and then the normal text .. .. . .

    Note: – There is only one space between the CHARACTERS of the word. But there is two or more than two spaces between the WORDS.

    I have tried using the string.replace function, but then I get problems with the normal text..

    here is the website from which I am getting the text : http://files.seds.org/pub/software/text/weather.txt

    PLEASE HELP ME OUT 🙁

  6. Steve D. says:

    Derek, When you execute the first search using \\s [A-Za-z]{2,20} \\s, I was expecting all words to be output, ie your last name was not in output. Why is that?

  7. Diana Magers says:

    Derek, Just wanted to Thank you so much. Your video help me understand the pattern expressions in working on a PhoneExpression.java program that I had to do for my java college course mid term.
    Just watched the video on enumeration that we are doing for thus weeks assignment. We are creating a program for Movie Theater Ticket system.
    Your videos boosts my confidence.
    So you can have the knowing that you are helping someone.

    Awesome work again.

  8. Ahmed nassef says:

    hi derek
    i really wanna thank you you made my life much easier with your tuts,i’m a java beginner and i have a problem hope you help me with it. i want to make an app in which i ask the user to input the network ip address and i want it in this format (*.*.*.*) how can i use regex to do it?, or if there is a simpler way please guide me.
    thanks in advance

Leave a Reply

Your email address will not be published.

Google+