1 00:00:00,000 --> 00:00:01,600 [no audio] 2 00:00:01,600 --> 00:00:03,400 Friends here our concept is 3 00:00:03,400 --> 00:00:06,200 'compile' operation or method of 're' module 4 00:00:06,400 --> 00:00:09,500 See we know that we have different types of operations in 5 00:00:09,500 --> 00:00:10,600 your 're' module. 6 00:00:11,300 --> 00:00:15,600 Let me go with some of them so that I can explain the usage 7 00:00:15,600 --> 00:00:17,200 of 'compile' operation. Suppose 8 00:00:17,200 --> 00:00:19,800 I have some string here, now 9 00:00:20,100 --> 00:00:26,400 I need to write a pattern such that I want to find 'python' 10 00:00:26,400 --> 00:00:28,000 is there a given string or not. 11 00:00:29,200 --> 00:00:31,000 Then just let me write a pattern. 12 00:00:32,000 --> 00:00:35,600 Suppose first I will go and work with 'findall' operation. 13 00:00:36,600 --> 00:00:40,400 How many 'python' are there in a given string? 14 00:00:40,400 --> 00:00:43,300 So for that I'm writing simply a pattern. See that, first 15 00:00:43,300 --> 00:00:47,000 I'm writing simply 'python'. But I want to look for a word, 16 00:00:47,000 --> 00:00:50,700 then you have to write with '\b' starting and ending, then 17 00:00:50,700 --> 00:00:53,700 exactly word matching. Now, first 18 00:00:53,700 --> 00:00:59,800 let me simply 'print(re.findall)', from your pattern 19 00:01:00,000 --> 00:01:02,500 in a given string how many matchings are there. 20 00:01:04,700 --> 00:01:08,400 See the result. You're getting only one 'python', but we have 21 00:01:08,400 --> 00:01:10,700 here 'python', and here also 'python'. 22 00:01:10,700 --> 00:01:13,400 Anyway, these are 'python2' and 'python3'. No problem. 23 00:01:14,300 --> 00:01:16,600 So here capital 'P', and here small 'p'. 24 00:01:16,700 --> 00:01:20,900 Then you can write in two ways to identify this small 'p' 'python', 25 00:01:20,900 --> 00:01:22,300 capital 'P' 'Python', that is 26 00:01:22,300 --> 00:01:25,800 if you write in square brackets, very first character 27 00:01:25,800 --> 00:01:28,500 it maybe small 'p', or capital 'P', [pP]. Now see the result. 28 00:01:28,500 --> 00:01:30,300 [no audio] 29 00:01:30,300 --> 00:01:33,900 Or instead of that, instead of that suppose 30 00:01:33,900 --> 00:01:37,700 if you don't want to consider about case sensitivity simply 31 00:01:37,700 --> 00:01:41,100 you can write a flag here. If you remember there is an option called 32 00:01:41,100 --> 00:01:43,300 'flags'. Simply write 're.I'. 33 00:01:43,700 --> 00:01:47,000 Now see the result. You're going to get. 'python', small and 34 00:01:47,000 --> 00:01:49,900 capital. Now, along with that 35 00:01:49,900 --> 00:01:55,100 I need to identify 'python2' or 'python3' also. Then at the 36 00:01:55,100 --> 00:02:00,500 end 2 or 3 is optional, right. Then there is a '?'. 37 00:02:00,500 --> 00:02:03,700 So the '?' meaning is either 2 or 3, 38 00:02:03,900 --> 00:02:05,700 maybe once or none, 39 00:02:05,800 --> 00:02:08,400 that is the meaning of your '?'. See we already 40 00:02:08,400 --> 00:02:10,900 discussed about the rules to create your patterns in our 41 00:02:10,900 --> 00:02:12,000 previous videos, right? 42 00:02:12,900 --> 00:02:15,500 So based on that I am creating this pattern. That's fine. 43 00:02:16,300 --> 00:02:18,700 Now, let me run this and see the result. Now you're going 44 00:02:18,700 --> 00:02:22,200 to get 'python', capital P 'Python', 'python2', and 'python3'. 45 00:02:22,500 --> 00:02:25,200 So that is the usage of patterns actually, 46 00:02:25,200 --> 00:02:27,300 I mean 'regex', Regular Expressions. 47 00:02:29,000 --> 00:02:30,100 That's fine. Now, 48 00:02:30,100 --> 00:02:35,200 suppose I am using different operations on my same pattern, 49 00:02:35,200 --> 00:02:37,200 suppose I am using 're.search', 50 00:02:37,200 --> 00:02:39,500 [no audio] 51 00:02:39,500 --> 00:02:45,500 your pattern, then in a given string. So what is your given 52 00:02:45,500 --> 00:02:47,800 string, this one. Now see the result. 53 00:02:48,000 --> 00:02:49,200 You're getting some object. 54 00:02:49,200 --> 00:02:50,300 Yes, there is a match. 55 00:02:51,200 --> 00:02:55,000 So if there is a match, then you can look for your required, 56 00:02:56,000 --> 00:02:58,800 I mean, what is the match and where is the matching position 57 00:02:58,800 --> 00:03:01,400 occurred in a given string, you can go with that like, 'start', 58 00:03:01,400 --> 00:03:02,700 'end', 'group', right, 59 00:03:02,900 --> 00:03:05,700 we have those operations. That's fine. 60 00:03:06,200 --> 00:03:08,600 Now, let me do one more operation. 61 00:03:08,800 --> 00:03:13,600 So I want to print, I want to split my given string based 62 00:03:13,600 --> 00:03:14,600 on my pattern. 63 00:03:14,800 --> 00:03:18,400 So what is my pattern? So normally on your strings you have 64 00:03:18,400 --> 00:03:21,800 a split operation, if you go with that you can able to split your given 65 00:03:21,800 --> 00:03:25,600 string based on one single string, but here I am going to 66 00:03:25,600 --> 00:03:29,400 divide my given string based on either 'python' or capital 'P' 67 00:03:29,400 --> 00:03:33,200 'python' or 'python2' or 'python3', simply wherever these four 68 00:03:33,200 --> 00:03:36,600 words are there in a given string there split will happen. 69 00:03:37,000 --> 00:03:40,100 So that is the usage of 'split' from your Regular Expression. 70 00:03:40,900 --> 00:03:43,300 Now, let me write your string and see the result. 71 00:03:44,600 --> 00:03:48,100 Yes. So likewise, you can use your different operations. 72 00:03:49,800 --> 00:03:54,700 Right. But here, suppose sometimes you are going to use same 73 00:03:54,700 --> 00:03:57,400 pattern multiple times in your given code, 74 00:03:57,800 --> 00:04:00,200 I mean, whatever the code you're writing, in your code 75 00:04:00,200 --> 00:04:02,200 you are going to use your pattern, 76 00:04:02,300 --> 00:04:04,900 whatever the pattern you have that pattern you are going to 77 00:04:04,900 --> 00:04:06,100 use multiple times. 78 00:04:06,500 --> 00:04:08,600 So whenever if you are using multiple times, 79 00:04:08,600 --> 00:04:10,300 instead of using in this way 80 00:04:12,000 --> 00:04:16,500 we are going with 'compile' operation. First see the code so 81 00:04:16,500 --> 00:04:19,200 that you can understand then I will explain you once again. 82 00:04:20,200 --> 00:04:24,800 Now, I am creating pattern object, pattern object. 83 00:04:25,100 --> 00:04:27,200 Nothing is there. Simply 're.compile', 84 00:04:28,000 --> 00:04:30,200 whatever the pattern you have. That's it. 85 00:04:31,600 --> 00:04:33,300 Now if you print your pattern object. 86 00:04:33,300 --> 00:04:37,100 [no audio] 87 00:04:37,100 --> 00:04:38,100 First see the result. 88 00:04:39,100 --> 00:04:40,100 What happened? 89 00:04:40,100 --> 00:04:45,100 [no audio] 90 00:04:45,100 --> 00:04:47,500 Sorry, spelling mistake. 91 00:04:49,200 --> 00:04:55,400 Yeah, see that. You are getting a pattern but in terms of object. 92 00:04:56,000 --> 00:05:00,700 Now, this is a object not like simply your 'python', something like 93 00:05:00,700 --> 00:05:05,100 that. Your 'Python' is converting now your pattern into an 94 00:05:05,100 --> 00:05:10,800 object. Now on this object instead of performing your operations 95 00:05:10,800 --> 00:05:13,600 in this way 're.search', 're.findall', 're.split', 96 00:05:13,600 --> 00:05:17,600 or whatever the operation you have, instead of that directly 97 00:05:17,600 --> 00:05:20,600 you can perform your operation on this object. 98 00:05:20,600 --> 00:05:23,400 [no audio] 99 00:05:23,400 --> 00:05:28,200 See that. Sorry, 'pat_obj', 'print', suppose 100 00:05:28,200 --> 00:05:31,600 'pat_obj.', your operation, 101 00:05:32,700 --> 00:05:35,600 then only string because already pattern is there in this 102 00:05:35,600 --> 00:05:38,800 object, only string, your string. 103 00:05:38,800 --> 00:05:42,400 [no audio] 104 00:05:42,400 --> 00:05:44,900 Now see the result. Whatever the output previously you got 105 00:05:44,900 --> 00:05:46,200 same output you're getting here. 106 00:05:46,200 --> 00:05:47,700 Let me enable 107 00:05:51,300 --> 00:05:54,700 your previous output, without 'compile' operation whatever 108 00:05:54,700 --> 00:05:57,000 the way we are using to use 'search', 109 00:05:57,000 --> 00:05:59,500 [no audio] 110 00:05:59,500 --> 00:06:01,600 is there any difference between these two, first output and 111 00:06:01,600 --> 00:06:03,200 last output? There is no difference. 112 00:06:03,400 --> 00:06:09,600 But while comparing first procedure, I mean sixth line syntax, 113 00:06:10,200 --> 00:06:13,300 your 13th line is having more efficiency. 114 00:06:14,500 --> 00:06:18,600 So speed of operation is very high if we use simply pattern objects. 115 00:06:18,600 --> 00:06:20,600 [no audio] 116 00:06:20,600 --> 00:06:23,500 Not only for that, you can use all operations. Now, 117 00:06:23,500 --> 00:06:27,800 let me disable your normal direct operations. 118 00:06:27,800 --> 00:06:31,100 Now I am using, you can use, for each and every operation you 119 00:06:31,100 --> 00:06:32,700 can use this pattern object. 120 00:06:33,800 --> 00:06:37,800 'pat_obj.findall', then simply your string. 121 00:06:38,200 --> 00:06:41,400 What is the String? Only pattern is there in this object guys. 122 00:06:42,400 --> 00:06:45,000 So now this pattern object is supporting to perform your 123 00:06:45,000 --> 00:06:47,500 all Regular Expressions without any 're' module. 124 00:06:48,500 --> 00:06:51,300 See that. Previously to perform your operations you're using 125 00:06:51,300 --> 00:06:55,000 're.', but now we are using 'pat_ob.'. So already 126 00:06:55,000 --> 00:06:57,900 inside of this 'pat_ob', your 're' is there and 127 00:06:57,900 --> 00:07:01,100 your 'compile' pattern is also there. Now directly we are doing 128 00:07:01,100 --> 00:07:03,600 our operation on that. See the result. 129 00:07:05,000 --> 00:07:09,200 'pat_ob.findall("python"). What happened? 130 00:07:09,200 --> 00:07:10,400 Where is it? Yeah. 131 00:07:12,000 --> 00:07:13,600 See that. Previously 132 00:07:13,800 --> 00:07:17,300 we used ignore case sensitivity, now in 'compile' operation 133 00:07:17,400 --> 00:07:20,800 you can also use flags then only you can get your remaining 134 00:07:21,100 --> 00:07:24,800 pythons, because we are looking for capital 'P', right. Now see 135 00:07:24,800 --> 00:07:31,200 that 'flags=re.I'. So 'compile' operation or method 136 00:07:31,200 --> 00:07:35,800 is also supporting 'flags' operations. Now see the result. 137 00:07:35,800 --> 00:07:38,500 [no audio] 138 00:07:38,500 --> 00:07:44,200 That's it. So guys, nothing is there. Let me write one simple, suppose 139 00:07:44,200 --> 00:07:49,200 if you are using 're.findall', your pattern, then your 140 00:07:49,200 --> 00:07:52,300 string, this we are doing in this way. 141 00:07:52,300 --> 00:07:54,900 [no audio] 142 00:07:54,900 --> 00:08:03,400 're.compile()', your pattern, '.findall'. This is exactly equal to this thing. 143 00:08:05,000 --> 00:08:06,900 I mean any operation you can compare in this way. 144 00:08:08,100 --> 00:08:09,900 So 're.findall', now 145 00:08:09,900 --> 00:08:11,600 we are doing 're.compile'. First 146 00:08:11,600 --> 00:08:15,600 we are creating an object using your patterns, on that object 147 00:08:15,600 --> 00:08:17,100 we are performing our operation. 148 00:08:17,900 --> 00:08:20,200 You can also store this output into some variable. 149 00:08:20,200 --> 00:08:21,200 That's what I did. 150 00:08:21,200 --> 00:08:23,300 [no audio] 151 00:08:23,300 --> 00:08:26,300 Right. Nothing is there. See most of the people will get confusion 152 00:08:26,300 --> 00:08:31,000 why we need to use 'compile' operation? Simple, whenever if 153 00:08:31,000 --> 00:08:34,700 you are using same pattern in different places in your code, 154 00:08:36,200 --> 00:08:41,600 then instead of in that way just try to compile your object, 155 00:08:41,600 --> 00:08:45,100 try to create 'compile' object first. On that object 156 00:08:45,100 --> 00:08:47,400 you just try to perform your required operation in 157 00:08:47,400 --> 00:08:51,400 this way, so that your code is efficient, speed of operation is high. 158 00:08:51,900 --> 00:08:53,100 Other than that nothing is there. 159 00:08:54,200 --> 00:08:55,500 Okay. Okay guys, 160 00:08:55,500 --> 00:08:56,900 thank you for watching this video. 161 00:08:56,900 --> 00:09:07,524 [no audio]