File size: 8,032 Bytes
33852e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
1
00:00:00,000 --> 00:00:00,000
Hello, team.

2
00:00:00,000 --> 00:00:04,000
In this lesson, I'd like to talk with you about regular expressions.

3
00:00:05,000 --> 00:00:06,000
What is regular expression?

4
00:00:06,000 --> 00:00:11,000
Regular expression is a sequence of characters that define a search pattern.

5
00:00:11,000 --> 00:00:16,000
So you can use special set of characters to find all strings that match your pattern.

6
00:00:17,000 --> 00:00:20,000
Sometimes you use regular expression to split string.

7
00:00:20,000 --> 00:00:22,000
Let's imagine next case.

8
00:00:22,000 --> 00:00:26,000
You have a lot of text and you need to find only Google email accounts.

9
00:00:27,000 --> 00:00:28,000
How you would do this?

10
00:00:28,000 --> 00:00:32,000
You will try to describe search pattern with the help of special symbols.

11
00:00:33,000 --> 00:00:35,000
What are the special symbols?

12
00:00:35,000 --> 00:00:36,000
Here are some of them.

13
00:00:36,000 --> 00:00:40,000
Square brackets to identify characters which we need to search.

14
00:00:41,000 --> 00:00:45,000
I specified range from A to z lowercase and from A to z capital.

15
00:00:46,000 --> 00:00:51,000
Also specified dash character backslash d will match with all digits.

16
00:00:51,000 --> 00:00:55,000
And you already learned that we need to escape backslash character.

17
00:00:55,000 --> 00:00:58,000
That's why it's double backslash here.

18
00:00:59,000 --> 00:01:01,000
Also there are quantifiers.

19
00:01:01,000 --> 00:01:03,000
Plus sign might be read like this.

20
00:01:03,000 --> 00:01:07,000
In case these characters will be repeated at least one time and more.

21
00:01:08,000 --> 00:01:10,000
That is what we need after all of this.

22
00:01:10,000 --> 00:01:14,000
We expect at sign gmail dot.

23
00:01:14,000 --> 00:01:21,000
Taking into account dot in regular expression means any character we need to escape dot to say that

24
00:01:21,000 --> 00:01:26,000
we don't expect any character, but we are looking for exactly dot.

25
00:01:26,000 --> 00:01:30,000
That's why double backslash here and comb.

26
00:01:30,000 --> 00:01:33,000
Your homework task will be to learn other special symbols.

27
00:01:34,000 --> 00:01:36,000
I will share with you details at the end of the lesson.

28
00:01:37,000 --> 00:01:43,000
Definitely, there are a lot of ways to improve the regular expressions for email, and you can find

29
00:01:43,000 --> 00:01:48,000
in internet billions of variations of pattern for email address verification.

30
00:01:48,000 --> 00:01:55,000
Our goal right now to learn how to use regular expressions regarding special symbols to use in regular

31
00:01:55,000 --> 00:01:55,000
expression.

32
00:01:56,000 --> 00:01:59,000
I will share more details at the end of this lesson.

33
00:01:59,000 --> 00:02:02,000
After that you need to compile pattern.

34
00:02:02,000 --> 00:02:06,000
Do not forget to import Java.util regex package.

35
00:02:09,000 --> 00:02:12,000
We have the sample text with gmail address in it.

36
00:02:12,000 --> 00:02:19,000
Now you can get matcher much object out of pattern call matching method on pattern object and pass sample

37
00:02:19,000 --> 00:02:20,000
text into it.

38
00:02:21,000 --> 00:02:25,000
Match object can find matches call find method.

39
00:02:25,000 --> 00:02:32,000
Find method returns true in case a subsequence of the input sequence matches this matches pattern and

40
00:02:32,000 --> 00:02:33,000
false otherwise.

41
00:02:33,000 --> 00:02:38,000
In case there is a match, you will be able to call group method to return the string which matches

42
00:02:38,000 --> 00:02:39,000
your pattern.

43
00:02:40,000 --> 00:02:42,000
And let's print this to console.

44
00:02:43,000 --> 00:02:45,000
What can be improved here?

45
00:02:46,000 --> 00:02:50,000
As you learn loops, topic and control statements topic, you will be able to manage search process

46
00:02:50,000 --> 00:02:52,000
in a bit different way.

47
00:02:52,000 --> 00:02:58,000
For example, probably better solution would be here to call find method in if statement or in while

48
00:02:58,000 --> 00:03:00,000
loop to print multiple matches.

49
00:03:01,000 --> 00:03:04,000
But don't worry so far we will learn all topics gradually.

50
00:03:04,000 --> 00:03:07,000
You will have enough tasks to practice your skills.

51
00:03:07,000 --> 00:03:10,000
For now, I don't want to confuse you even more.

52
00:03:10,000 --> 00:03:15,000
Solution like this should be fine in case you want to find match in the string.

53
00:03:16,000 --> 00:03:17,000
Another example.

54
00:03:17,000 --> 00:03:21,000
We have sample text and we want to split it by sentences.

55
00:03:21,000 --> 00:03:23,000
Let's come up with a regular expression.

56
00:03:23,000 --> 00:03:26,000
We need to split by any of these characters.

57
00:03:26,000 --> 00:03:27,000
Period.

58
00:03:27,000 --> 00:03:29,000
Exclamation mark.

59
00:03:29,000 --> 00:03:30,000
Question mark.

60
00:03:31,000 --> 00:03:33,000
Now let's split this sample text.

61
00:03:33,000 --> 00:03:36,000
Using this regex we'll call split method.

62
00:03:36,000 --> 00:03:40,000
We'll get string array and let's print it to console.

63
00:03:41,000 --> 00:03:44,000
Here you can see that you have three different sentences.

64
00:03:45,000 --> 00:03:47,000
Let's recap what you have learned about strings.

65
00:03:48,000 --> 00:03:52,000
Now you know main string methods how to compare strings.

66
00:03:52,000 --> 00:03:54,000
You learn what is pool of strings.

67
00:03:54,000 --> 00:04:00,000
How to use escape sequences in your source code, how you can format strings, and how to use regular

68
00:04:00,000 --> 00:04:01,000
expressions.

69
00:04:01,000 --> 00:04:03,000
Now let's take a look at your homework.

70
00:04:05,000 --> 00:04:09,000
First of all, take a look one more time to string source code.

71
00:04:09,000 --> 00:04:12,000
Take a look at other methods which are in string class.

72
00:04:13,000 --> 00:04:15,000
As usual, I recommend you to read books.

73
00:04:15,000 --> 00:04:17,000
Chapter about strings.

74
00:04:17,000 --> 00:04:20,000
Learn format specifiers and flags.

75
00:04:20,000 --> 00:04:23,000
Here is the link to official Oracle documentation.

76
00:04:24,000 --> 00:04:29,000
I also did screenshots and left them here so you could faster find the things you need to learn.

77
00:04:30,000 --> 00:04:33,000
Do not forget to look through the examples of string formatting.

78
00:04:34,000 --> 00:04:36,000
Link to the source code is here.

79
00:04:36,000 --> 00:04:39,000
Implement console program according to requirements.

80
00:04:40,000 --> 00:04:43,000
This task to check your knowledge about string formatting.

81
00:04:44,000 --> 00:04:47,000
To learn regular expressions, I recommend you to use this resource.

82
00:04:47,000 --> 00:04:49,000
Let's take a look.

83
00:04:50,000 --> 00:04:51,000
You have 15 lessons.

84
00:04:51,000 --> 00:04:55,000
In each lesson you have description and exercise.

85
00:04:55,000 --> 00:04:58,000
You can always find hints on the right here.

86
00:04:58,000 --> 00:05:01,000
And in case you can't solve task.

87
00:05:01,000 --> 00:05:03,000
Take a look at solution here.

88
00:05:07,000 --> 00:05:13,000
Looking through the solutions will be also very efficient and will help you to learn regular expressions.

89
00:05:13,000 --> 00:05:19,000
I wouldn't say that you need to spend hours on learning regular expressions because you will not use

90
00:05:19,000 --> 00:05:20,000
them literally every day.

91
00:05:20,000 --> 00:05:23,000
But still, I would recommend you to learn the basics.

92
00:05:24,000 --> 00:05:30,000
And after that you have one more coding exercise to practice your skills in regular expressions.

93
00:05:30,000 --> 00:05:32,000
That's all for today.

94
00:05:32,000 --> 00:05:33,000
See you in the next lesson.

95
00:05:34,000 --> 00:05:34,000
Your.