1 00:00:00,000 --> 00:00:03,300 Friends, we have been working with the rules to create your 2 00:00:03,300 --> 00:00:05,200 pattern. Now already 3 00:00:05,200 --> 00:00:08,000 we have seen some of the rules to create your pattern. 4 00:00:08,100 --> 00:00:10,800 Like these are the first set of rules to create your pattern, 5 00:00:11,000 --> 00:00:13,900 and these are the second set of rules to create your pattern. 6 00:00:14,700 --> 00:00:17,000 Now, we are going to work with some more rules. 7 00:00:17,800 --> 00:00:19,600 Now let me take the very first rule, 8 00:00:19,600 --> 00:00:22,800 is curly braces inside that some number. 9 00:00:23,500 --> 00:00:26,800 So what is the meaning for this is, exactly two times. 10 00:00:26,800 --> 00:00:29,900 Now, let me open my editor to get clarity on this usage. 11 00:00:30,100 --> 00:00:33,800 So I have saved this script as 'rules_3.py'. 12 00:00:33,800 --> 00:00:36,600 First of all, let me import my 're' module. 13 00:00:37,700 --> 00:00:40,500 Now just assume that we have a text in this way. 14 00:00:40,500 --> 00:00:42,900 [no audio] 15 00:00:42,900 --> 00:00:45,100 "This is a python". 16 00:00:45,100 --> 00:00:48,200 [no audio] 17 00:00:48,200 --> 00:00:50,100 and python3". 18 00:00:51,300 --> 00:00:54,600 Just assume that. Now what is my requirement 19 00:00:54,600 --> 00:00:57,000 is, suppose if I take my pattern as, 20 00:00:58,400 --> 00:00:59,500 so guys I am using from 21 00:00:59,500 --> 00:01:01,200 now onwards 'raw string', simply 'r'. 22 00:01:02,400 --> 00:01:06,900 Suppose if I try to look for 'python', or even let me remove this. 23 00:01:06,900 --> 00:01:09,200 [no audio] 24 00:01:09,200 --> 00:01:10,400 Now let me 'print()', 25 00:01:10,400 --> 00:01:12,900 [no audio] 26 00:01:12,900 --> 00:01:16,700 your 're.findall()', your pattern in a given text. 27 00:01:16,900 --> 00:01:21,200 Now see the result. You're getting 'python' two times, but in 'python' 28 00:01:21,200 --> 00:01:25,800 at the ending I have 'n' two times. I want to get that, so what I 29 00:01:25,800 --> 00:01:29,600 I can do? I can write 'n' two times. Now, I am going to get that. Yes. 30 00:01:30,800 --> 00:01:32,800 Suppose I have a 'python', 31 00:01:34,400 --> 00:01:38,000 just some character four times then I want to get this entire 32 00:01:38,000 --> 00:01:41,000 matching, then see that. Suppose 33 00:01:41,000 --> 00:01:44,600 I want to look for a 'python' such that 'n' is four times, 34 00:01:44,700 --> 00:01:49,700 I want to look for a 'python' word such that 'n' is four times. If 35 00:01:49,700 --> 00:01:54,600 it is a word you should write '\b' before, on starting. Do 36 00:01:54,600 --> 00:01:57,100 we have that? No, nowhere we have 'python' 37 00:01:58,300 --> 00:02:02,200 with two 'n' word, but we have a word with 'python', 'n' four 38 00:02:02,200 --> 00:02:04,100 times. Then let me write 'n' four times. 39 00:02:04,100 --> 00:02:07,000 [no audio] 40 00:02:07,000 --> 00:02:10,400 Now see the result. Yes, you are getting. But actually here 'n' 41 00:02:10,400 --> 00:02:13,500 is four times no. No need to write four times your 'n', simply 42 00:02:13,500 --> 00:02:17,300 write 'n', then ''. 43 00:02:17,300 --> 00:02:21,000 Now the meaning for this is 'n' is four times. That's it. 44 00:02:21,000 --> 00:02:23,700 [no audio] 45 00:02:23,700 --> 00:02:26,200 Likewise you can apply for any character. 46 00:02:26,800 --> 00:02:28,000 Let's say, suppose 47 00:02:28,100 --> 00:02:29,600 let me comment this pattern. 48 00:02:30,600 --> 00:02:33,900 I am writing somewhere suppose simply 'aa' 49 00:02:33,900 --> 00:02:35,900 [no audio] 50 00:02:35,900 --> 00:02:37,000 then something 51 00:02:37,000 --> 00:02:38,800 then in this way. 52 00:02:40,200 --> 00:02:43,700 Now here also suppose I have 'a', just assume that 53 00:02:43,700 --> 00:02:45,700 three times 'a', here also three times 'a'. 54 00:02:45,700 --> 00:02:47,700 [no audio] 55 00:02:47,700 --> 00:02:51,500 Now just based on our previous rules and current rules 56 00:02:52,000 --> 00:02:56,700 what I want to do is, I want to get a word which consists 57 00:02:56,700 --> 00:02:58,000 of 'a' three times. 58 00:02:58,000 --> 00:02:59,800 [no audio] 59 00:02:59,800 --> 00:03:04,600 Then first of all I am trying to find the word. Whenever 60 00:03:04,600 --> 00:03:06,900 there is a word you have to write '\b' at the starting 61 00:03:06,900 --> 00:03:09,600 and ending. Then between that 62 00:03:10,700 --> 00:03:13,300 you're looking for a word called 'aaa'. 63 00:03:14,000 --> 00:03:16,400 Yes, you are getting that. Only one time you are having. 64 00:03:17,500 --> 00:03:20,400 But 'a' is three times no, why should we write 'a' three times, 65 00:03:20,400 --> 00:03:23,100 simply you can write 'a', in curly braces 66 00:03:23,100 --> 00:03:25,500 you can also write three, ''. That's it. 67 00:03:26,900 --> 00:03:34,400 Now I want to look for a word such that 'a' is 1, 2, 3, 4, 5, 6, 7, 8; 68 00:03:37,000 --> 00:03:38,400 the word which is having, 69 00:03:38,400 --> 00:03:40,200 [no audio] 70 00:03:40,200 --> 00:03:43,900 the word with 'a' eight times. That's it. 71 00:03:45,300 --> 00:03:49,200 Suppose sometimes you may have starting suppose some 'xyz'. 72 00:03:50,900 --> 00:03:53,300 Now you don't have word with 'a' eight times. 73 00:03:53,400 --> 00:03:57,500 You have a word with 'xyz', after that 'a' eight times. Then before 74 00:03:57,500 --> 00:03:59,400 'a' what we have? 'xyz'. 75 00:04:00,600 --> 00:04:04,500 Now see the pattern what I have taken, 'xyz', after that 'a' eight 76 00:04:04,500 --> 00:04:07,000 times. That type of word I want to find. 77 00:04:08,400 --> 00:04:09,400 That's it. 78 00:04:10,200 --> 00:04:15,800 So that is the use of curly braces with the number. Then ''. 79 00:04:13,200 --> 00:04:17,600 [no audio] 80 00:04:17,600 --> 00:04:22,200 See here, if I write it '', exactly two times. If I write '', 81 00:04:22,200 --> 00:04:25,800 then whatever the character before this curly braces you 82 00:04:25,800 --> 00:04:28,800 have, that character maybe two times, or maybe three times, 83 00:04:28,800 --> 00:04:30,000 or maybe four times. 84 00:04:31,600 --> 00:04:32,700 See that. What I am, 85 00:04:33,400 --> 00:04:34,900 let me take a new text. 86 00:04:36,600 --> 00:04:38,300 I will do one thing, I will comment this. 87 00:04:38,300 --> 00:04:41,300 [no audio] 88 00:04:41,300 --> 00:04:43,300 Suppose let us say I have a text called 89 00:04:43,300 --> 00:04:45,300 [no audio] 90 00:04:45,300 --> 00:04:47,800 'xaz', something, 91 00:04:47,800 --> 00:04:49,300 'xaaz', 92 00:04:49,300 --> 00:04:58,100 [no audio] 93 00:04:58,100 --> 00:04:59,100 Then let's say. 94 00:04:59,100 --> 00:05:01,100 [no audio] 95 00:05:01,100 --> 00:05:13,300 'xaaaa', sometimes, 'z', then 'x', 1, 2, 3, 4, let me take three 'z', then 'x'. 96 00:05:15,000 --> 00:05:16,600 Let me take four times also, no problem. 97 00:05:16,700 --> 00:05:21,700 Now our understanding is, now I want to find a word such that 98 00:05:22,000 --> 00:05:25,000 the word should have starting letter is 'x' and ending letter 99 00:05:25,000 --> 00:05:26,300 is 'z'. First 100 00:05:26,300 --> 00:05:27,600 let me write pattern for that. 101 00:05:28,900 --> 00:05:31,000 First of all I'm looking for a word, that's why 102 00:05:32,300 --> 00:05:36,300 '\b \b', starting and ending you should have space, and then my word 103 00:05:36,300 --> 00:05:39,600 should have starting letter as 'x', and ending letter as 'z', 104 00:05:40,000 --> 00:05:44,300 but between this I have 'a'. Now, I need to look for a word 105 00:05:44,300 --> 00:05:46,200 such that it is 'xaz'. 106 00:05:47,200 --> 00:05:49,000 Right. First, let me search in this way. 107 00:05:49,000 --> 00:05:52,000 [no audio] 108 00:05:52,000 --> 00:05:55,100 'findall', your pattern, in your text. 109 00:05:56,400 --> 00:05:57,900 Yes, 'xaz' you are getting. 110 00:05:57,900 --> 00:05:59,800 [no audio] 111 00:05:59,800 --> 00:06:01,600 Now my requirement is, 112 00:06:03,000 --> 00:06:05,600 between these two characters 'a' maybe 113 00:06:05,600 --> 00:06:12,100 one time, or two times, or three times, then simply after 'a' 114 00:06:12,100 --> 00:06:16,300 you have to write one time, or two times, or three times, or 115 00:06:16,300 --> 00:06:18,000 four times. That's it. 116 00:06:19,100 --> 00:06:20,100 Now see the result. 117 00:06:21,700 --> 00:06:24,800 If you have 'x' after that, between these two characters 118 00:06:24,800 --> 00:06:28,000 if you have 'a' one time, yes, it is going to fetch that. If 'a' 119 00:06:28,000 --> 00:06:30,000 is two times, it is trying to fetch that. 120 00:06:31,000 --> 00:06:33,600 Do we have that? No. Here 121 00:06:33,600 --> 00:06:37,300 we have, but here this character, 'z' character is two times. 122 00:06:37,300 --> 00:06:40,700 Now, let me remove one 'z', so that you can get that as well. See that. 123 00:06:40,700 --> 00:06:42,800 [no audio] 124 00:06:42,800 --> 00:06:45,800 That's it. So that is the use of your curly braces. 125 00:06:46,800 --> 00:06:50,100 So exactly for number of times you can write curly braces 126 00:06:50,100 --> 00:06:53,300 with that number, may be one times or two times or three times 127 00:06:53,300 --> 00:06:54,500 before your character, 128 00:06:54,500 --> 00:06:57,900 sorry the character whatever you are looking, then after your 129 00:06:57,900 --> 00:07:00,400 character you have to write in curly braces '', 130 00:07:00,700 --> 00:07:02,700 two or three or four times. 131 00:07:02,700 --> 00:07:04,500 [no audio] 132 00:07:04,500 --> 00:07:07,000 Then two or more times. 133 00:07:07,700 --> 00:07:11,200 I don't know how many times 'a' is there between your two characters, 134 00:07:11,500 --> 00:07:17,200 but I want to search minimum with the two letters, minimum 135 00:07:17,200 --> 00:07:21,300 with two times 'a', and then maximum any number of times. 136 00:07:21,300 --> 00:07:22,600 Now see the result. 137 00:07:23,700 --> 00:07:27,000 Here two times is there, and here some number of times is 138 00:07:27,000 --> 00:07:29,100 there, here some number of times is there. All are going 139 00:07:29,100 --> 00:07:32,700 to fetch because two times or more than that, three times, or 140 00:07:32,700 --> 00:07:37,600 four times, or five times, or infinite times between your 'a' and 'z'. 141 00:07:38,200 --> 00:07:41,500 Sorry your 'x' and 'z' your 'a' maybe two times, or 142 00:07:41,500 --> 00:07:44,800 three times, or four times, or infinite number of times. That's it. 143 00:07:44,800 --> 00:07:47,500 [no audio] 144 00:07:47,500 --> 00:07:53,500 Now '+', one or more. See forget about '+' from this concept 145 00:07:53,500 --> 00:07:56,000 if I want to try to look for one or more times 146 00:07:56,000 --> 00:07:58,500 I can write '1,' in curly braces, ''. 147 00:08:00,200 --> 00:08:01,200 See that 148 00:08:03,700 --> 00:08:05,100 'my_pat' is, 149 00:08:05,100 --> 00:08:13,000 [no audio] 150 00:08:13,000 --> 00:08:17,300 I am writing 'my_pat' as 'xa', 'a' 151 00:08:17,300 --> 00:08:19,400 maybe one time or more than that, 152 00:08:20,000 --> 00:08:22,000 then 'z'. Now see the result. 153 00:08:22,100 --> 00:08:25,600 You're going to get all your words which are having 154 00:08:25,600 --> 00:08:27,100 between 'x' and 'z', 'a'. 155 00:08:27,100 --> 00:08:29,000 [no audio] 156 00:08:29,000 --> 00:08:30,000 '1,' 157 00:08:32,400 --> 00:08:36,400 curly braces with '1,', ''. But same thing you can also 158 00:08:36,400 --> 00:08:38,000 write simply by '+'. 159 00:08:39,799 --> 00:08:44,200 Guys, whatever the symbol we're using, I am using here, that is 160 00:08:44,200 --> 00:08:46,500 for your previous character. 161 00:08:47,100 --> 00:08:51,500 This 'a' maybe one or more times, right. Be clear, 'a' is one or 162 00:08:51,500 --> 00:08:52,500 more times. 163 00:08:53,700 --> 00:08:54,700 That's it. 164 00:08:54,700 --> 00:08:57,700 [no audio] 165 00:08:57,700 --> 00:09:02,000 'a' is one or more times, but same thing you can also write 166 00:09:02,000 --> 00:09:03,000 in this way also. 167 00:09:03,000 --> 00:09:04,800 [no audio] 168 00:09:04,800 --> 00:09:05,800 Both are same. 169 00:09:05,800 --> 00:09:09,200 [no audio] 170 00:09:09,200 --> 00:09:16,500 Now '0 or more times'. See '*' means 0 or more times. Suppose 171 00:09:16,500 --> 00:09:22,800 if I take any word, which is having 'xz'. There is no 'a', right. As of now 172 00:09:22,800 --> 00:09:28,000 if I try to look with 'xa', or let me write 173 00:09:28,000 --> 00:09:29,800 same thing with '+' as well, 174 00:09:31,800 --> 00:09:37,800 'xa+z'. Now see the result. 175 00:09:38,600 --> 00:09:44,300 Same output, but my intention is between your 'x' and 'z', 'a' may 176 00:09:44,300 --> 00:09:45,300 or may not be. 177 00:09:46,800 --> 00:09:50,600 Maybe 0 times means there is no 'a', or maybe more than that. 178 00:09:52,200 --> 00:09:58,400 I mean 0 times or more than, more times, maybe one time, 179 00:09:58,400 --> 00:10:00,400 two times, three times, or four times, then simply 180 00:10:00,400 --> 00:10:05,300 you can take 'a*'. Now see the result. You're going to get 181 00:10:05,300 --> 00:10:07,200 this matching as well for that. 182 00:10:07,700 --> 00:10:12,000 So guys if I write in this way the possible patterns are 'xz', 183 00:10:12,300 --> 00:10:17,300 because '*' means 'a' may be 0 times or your 'a' is one 184 00:10:17,300 --> 00:10:24,900 time or your 'a' is two times, or your 'a' is three times, likewise. 185 00:10:25,200 --> 00:10:29,200 That is the meaning for '*'. '+' means except this first, 186 00:10:29,200 --> 00:10:30,600 remaining all are for '+'. 187 00:10:34,500 --> 00:10:41,600 Then the last one is 'once or none', '?'. Maybe one 188 00:10:41,600 --> 00:10:46,200 time or none time. Not for 0 times or more than. 0 times 189 00:10:46,200 --> 00:10:50,200 or one time, that is for square brackets. Now see that. Here 190 00:10:50,200 --> 00:10:54,200 'a' is there one time between 'x' and 'z', here 0 times it is there, but here 191 00:10:54,200 --> 00:10:56,700 we have multiple times. But if I go with 192 00:10:59,700 --> 00:11:00,700 I'm creating pattern 193 00:11:00,700 --> 00:11:07,900 as raw string, r'xa?z'. That 'a' maybe 0 times, 194 00:11:08,100 --> 00:11:10,200 or one time, once or none. 195 00:11:10,300 --> 00:11:11,800 That's it. Now see the result. 196 00:11:12,900 --> 00:11:16,800 Whichever is having 'a' one-time or 0 times, those things 197 00:11:16,800 --> 00:11:18,900 we are getting. That's it. 198 00:11:18,900 --> 00:11:21,200 [no audio] 199 00:11:21,200 --> 00:11:25,600 So the possible words for this is, 'x', '?' means 0 200 00:11:25,600 --> 00:11:30,900 times, 0 times 'a', 'z'. Then 'x', one time maybe 'a'. That's it. 201 00:11:30,900 --> 00:11:32,200 These two are the possible words. 202 00:11:32,200 --> 00:11:33,700 Now these two words 203 00:11:33,700 --> 00:11:36,800 are there in a given text here and here. So that's why it is fetching them. 204 00:11:37,900 --> 00:11:38,900 That's it. 205 00:11:38,900 --> 00:11:40,800 [no audio] 206 00:11:40,800 --> 00:11:42,000 Right. So guys 207 00:11:42,000 --> 00:11:45,300 these are the some more simple rules to create your pattern, 208 00:11:45,300 --> 00:11:47,400 and these are very, very important. 209 00:11:47,400 --> 00:11:49,700 [no audio] 210 00:11:49,700 --> 00:11:52,200 See whatever the rule we have here, 211 00:11:52,200 --> 00:11:56,100 and this rule we are using in your pattern, but make sure 212 00:11:56,100 --> 00:12:00,200 that whenever if you are applying these rules, these rules 213 00:12:00,200 --> 00:12:04,200 are applicable for your particular character, not for your entire words. 214 00:12:04,200 --> 00:12:07,700 See I am using for 'a' this '?' means, this symbol, 215 00:12:07,700 --> 00:12:11,100 this rule is applicable for this 'a'. Somewhere suppose 216 00:12:11,100 --> 00:12:14,700 we used here '', now for this 'n', 217 00:12:14,700 --> 00:12:18,000 not for your entire word, be clear, not for your entire word, 218 00:12:18,000 --> 00:12:19,500 this is for only 'n' character 219 00:12:19,700 --> 00:12:21,600 you are applying this concept. 220 00:12:21,600 --> 00:12:24,270 [no audio] 221 00:12:24,200 --> 00:12:27,900 Right. Okay guys, thank you for watching this video. 222 00:12:27,900 --> 00:12:33,000 [no audio]