Hello, team.
In this lesson, I'd like to talk with you about regular expressions.
What is regular expression?
Regular expression is a sequence of characters that define a search pattern.
So you can use special set of characters to find all strings that match your pattern.
Sometimes you use regular expression to split string.
Let's imagine next case.
You have a lot of text and you need to find only Google email accounts.
How you would do this?
You will try to describe search pattern with the help of special symbols.
What are the special symbols?
Here are some of them.
Square brackets to identify characters which we need to search.
I specified range from A to z lowercase and from A to z capital.
Also specified dash character backslash d will match with all digits.
And you already learned that we need to escape backslash character.
That's why it's double backslash here.
Also there are quantifiers.
Plus sign might be read like this.
In case these characters will be repeated at least one time and more.
That is what we need after all of this.
We expect at sign gmail dot.
Taking into account dot in regular expression means any character we need to escape dot to say that
we don't expect any character, but we are looking for exactly dot.
That's why double backslash here and comb.
Your homework task will be to learn other special symbols.
I will share with you details at the end of the lesson.
Definitely, there are a lot of ways to improve the regular expressions for email, and you can find
in internet billions of variations of pattern for email address verification.
Our goal right now to learn how to use regular expressions regarding special symbols to use in regular
expression.
I will share more details at the end of this lesson.
After that you need to compile pattern.
Do not forget to import Java.util regex package.
We have the sample text with gmail address in it.
Now you can get matcher much object out of pattern call matching method on pattern object and pass sample
text into it.
Match object can find matches call find method.
Find method returns true in case a subsequence of the input sequence matches this matches pattern and
false otherwise.
In case there is a match, you will be able to call group method to return the string which matches
your pattern.
And let's print this to console.
What can be improved here?
As you learn loops, topic and control statements topic, you will be able to manage search process
in a bit different way.
For example, probably better solution would be here to call find method in if statement or in while
loop to print multiple matches.
But don't worry so far we will learn all topics gradually.
You will have enough tasks to practice your skills.
For now, I don't want to confuse you even more.
Solution like this should be fine in case you want to find match in the string.
Another example.
We have sample text and we want to split it by sentences.
Let's come up with a regular expression.
We need to split by any of these characters.
Period.
Exclamation mark.
Question mark.
Now let's split this sample text.
Using this regex we'll call split method.
We'll get string array and let's print it to console.
Here you can see that you have three different sentences.
Let's recap what you have learned about strings.
Now you know main string methods how to compare strings.
You learn what is pool of strings.
How to use escape sequences in your source code, how you can format strings, and how to use regular
expressions.
Now let's take a look at your homework.
First of all, take a look one more time to string source code.
Take a look at other methods which are in string class.
As usual, I recommend you to read books.
Chapter about strings.
Learn format specifiers and flags.
Here is the link to official Oracle documentation.
I also did screenshots and left them here so you could faster find the things you need to learn.
Do not forget to look through the examples of string formatting.
Link to the source code is here.
Implement console program according to requirements.
This task to check your knowledge about string formatting.
To learn regular expressions, I recommend you to use this resource.
Let's take a look.
You have 15 lessons.
In each lesson you have description and exercise.
You can always find hints on the right here.
And in case you can't solve task.
Take a look at solution here.
Looking through the solutions will be also very efficient and will help you to learn regular expressions.
I wouldn't say that you need to spend hours on learning regular expressions because you will not use
them literally every day.
But still, I would recommend you to learn the basics.
And after that you have one more coding exercise to practice your skills in regular expressions.
That's all for today.
See you in the next lesson.
Your.