In 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 [nttfblike]
Code From the Video
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
Hi Derek,
Considering that its a “word”, wont this work?
“^[aA][a-zA-Z]*[bB]$”
Thanks,
Pralay
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
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
I’m very glad you figured it out 🙂 Sorry I couldn’t get to your question quicker
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
It breaks the results out so you can group them. You use it when you want to save a bunch of results into an array for example
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
In my opinion if you don’t know any other language it is probably better to start by learning Python. I have a python video tutorial to help you out. Please leave any questions you have. I’m happy to help 🙂
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
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?
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 🙁
Hello,
I just tested this and it works ([A-Z]+(?=\s[A-Z])(?:\s[A-Z]+)+)
I hope that helps 🙂
Derek
But Derek I want to end the single space that exists between the letters of these words. Please tell me how to get rid of that ?
Thank you.
I’m sorry, but I don’t understand the question.
i am asking that what should i do in order to remove the single white space character between the letters so that i can display them as one word .. like this :
SENTINELS ARE IN THE SKY
You just need to replace all single spaces with nothing. Then replace occurrences of multiple spaces with 1 space. Take a look at replaceAll here.
It worked! Thank you so much Derek.. You just made my life easy 🙂
I’m very happy that I could help 🙂
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?
That is because the space was eaten up when Derek was found
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.
Thank you for taking the time to tell me I helped 🙂 I appreciate it.
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
I’m glad I can help. Here is the regex for ip addresses \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b