1 00:00:00,000 --> 00:00:00,800 [no audio] 2 00:00:00,800 --> 00:00:02,200 Friends here we are going to 3 00:00:02,200 --> 00:00:04,300 work with Python Regular Expressions with 4 00:00:04,300 --> 00:00:05,800 Flags. See already 5 00:00:05,800 --> 00:00:10,000 we have seen the rules to create your patterns, and these 6 00:00:10,000 --> 00:00:13,400 are the rules which we saw to create your patterns. 7 00:00:13,400 --> 00:00:15,900 [no audio] 8 00:00:15,900 --> 00:00:19,300 Right. Now we are going to work with your Regular Expressions 9 00:00:19,300 --> 00:00:20,600 with flags as well. 10 00:00:21,300 --> 00:00:24,100 So these are the available flags for your Python Regular 11 00:00:24,100 --> 00:00:29,000 Expressions. See these are the flags. You can use either shortcut 12 00:00:29,100 --> 00:00:34,700 or this flag name, both are same. Now, among all those as of 13 00:00:34,700 --> 00:00:38,400 now I am going to work and I'm going to show you with 're.i' 14 00:00:38,400 --> 00:00:41,100 and 're.m'. Later we'll work with the remaining as 15 00:00:41,100 --> 00:00:46,200 well. Now first of all, 're.i' or 're.IGNORECASE'. 16 00:00:46,200 --> 00:00:48,500 You can use either this shortcut or this full-length 17 00:00:49,600 --> 00:00:53,400 flag. Makes the regular expression case-insensitive. 18 00:00:54,200 --> 00:00:56,800 So let me open my editor. To get clarity on this 19 00:00:56,900 --> 00:00:58,600 I am writing a simple script. 20 00:00:58,600 --> 00:01:00,500 [no audio] 21 00:01:00,500 --> 00:01:02,000 'import re' module. 22 00:01:02,700 --> 00:01:04,400 Let me take some 'text= 23 00:01:05,900 --> 00:01:06,900 "this is a 24 00:01:09,500 --> 00:01:13,400 string"', and I'm writing "ThIs 25 00:01:13,400 --> 00:01:15,300 [no audio] 26 00:01:15,300 --> 00:01:17,400 is a new string", something like that. 27 00:01:17,500 --> 00:01:19,900 Then I'm writing simply all are capitals. 28 00:01:19,900 --> 00:01:22,100 [no audio] 29 00:01:22,100 --> 00:01:28,100 Now see that my requirement is, I need to find all the strings 30 00:01:28,800 --> 00:01:29,800 with "this". 31 00:01:31,500 --> 00:01:32,200 "this" string 32 00:01:32,200 --> 00:01:35,600 I need to find. Suppose if I take a pattern as in this way, 33 00:01:35,600 --> 00:01:39,700 'my_pat' as, raw string I'm taking, so that is a good practice. 34 00:01:39,700 --> 00:01:42,800 We just try to take 'r' before writing your pattern. Then 35 00:01:42,800 --> 00:01:45,000 let me print 're.findall' 36 00:01:46,700 --> 00:01:48,700 with your pattern in a given 'text'. 37 00:01:48,900 --> 00:01:51,500 What is the result you are getting? What happened? 38 00:01:51,500 --> 00:01:53,500 [no audio] 39 00:01:53,500 --> 00:01:54,800 Sorry, 'findall'. 40 00:01:56,500 --> 00:02:01,600 We're getting, 'this'. But if you don't consider case sensitivity, 41 00:02:01,600 --> 00:02:07,300 then 'this' is a 'this', and see that you have 'this' here, and 42 00:02:07,300 --> 00:02:12,000 here, and here. But I am looking for small 't-h-i-s'. But they 43 00:02:12,000 --> 00:02:14,500 are combination with small and capital and these are completely 44 00:02:14,500 --> 00:02:15,800 capital. Now, 45 00:02:15,800 --> 00:02:19,200 I don't want to look for case sensitivity, then just include 46 00:02:19,200 --> 00:02:21,400 a flag called 're.I'. 47 00:02:21,400 --> 00:02:22,600 Now see the result. 48 00:02:23,700 --> 00:02:25,300 See here I included a flag. 49 00:02:25,300 --> 00:02:27,200 [no audio] 50 00:02:27,200 --> 00:02:31,000 So you can use either 're.I' shortcut of your flag, or 51 00:02:32,000 --> 00:02:33,200 'IGNORECASE'. 52 00:02:33,600 --> 00:02:36,300 That is also same. See that. Let me rerun and see the output. 53 00:02:37,500 --> 00:02:40,700 So just use a shortcut. That's better. 54 00:02:41,400 --> 00:02:42,400 That's it. 55 00:02:42,600 --> 00:02:46,400 So this is the use of your 'IGNORECASE' sensitivity flag. 56 00:02:48,000 --> 00:02:54,000 Then we have next thing 're.M', 'MULTILINE'. See that. First 57 00:02:54,000 --> 00:02:56,600 of all let me take some multi-line text. 58 00:02:56,600 --> 00:03:00,000 [no audio] 59 00:03:00,000 --> 00:03:03,200 Let me take my 'text' as, so guys 60 00:03:03,200 --> 00:03:04,200 this is useful, 61 00:03:04,200 --> 00:03:07,100 I mean this option, 're.m' is useful whenever if you are 62 00:03:07,100 --> 00:03:12,000 going to work with files using your file object or read operation. 63 00:03:12,700 --> 00:03:15,100 Anyhow we will see that example as well. First 64 00:03:15,100 --> 00:03:17,900 let me write some string with multiple lines. 65 00:03:18,100 --> 00:03:20,100 So, "this is a string", 66 00:03:21,200 --> 00:03:24,300 and, "this is a second line", something like that. 67 00:03:24,300 --> 00:03:27,100 I am writing, and, "This is a", suppose 68 00:03:27,100 --> 00:03:30,200 [no audio] 69 00:03:30,200 --> 00:03:35,600 third line", and this. So if you have a string in this way, first 70 00:03:35,600 --> 00:03:39,800 of all this is a multi-line string. You can print and see the result. 71 00:03:39,900 --> 00:03:42,100 See you are getting output in multi lines. 72 00:03:42,300 --> 00:03:46,400 So we are giving in terms of triple quotation your string, 73 00:03:46,400 --> 00:03:48,200 that means that is a multi-line string. 74 00:03:49,800 --> 00:03:55,400 Now, what I am doing is, I am writing a pattern just to find 75 00:03:56,000 --> 00:04:06,100 'this'. 'print(re.findall)', from your pattern in the given 76 00:04:06,100 --> 00:04:07,900 multi-line text. See the result. 77 00:04:09,000 --> 00:04:11,200 Let me comment this 'print(text)'. 78 00:04:12,900 --> 00:04:16,700 Now see the result. Wherever 'this' is there you are getting that. 79 00:04:18,399 --> 00:04:21,700 Okay. Anyway, I'm looking as of now suppose small 't-h-i-s' string. 80 00:04:22,000 --> 00:04:24,399 Yes, you are getting that. Now, 81 00:04:24,800 --> 00:04:29,600 I want to print if 'this' is there at very first in a given 82 00:04:29,600 --> 00:04:33,900 string. Then I can take '^' symbol, right. See the result. 83 00:04:35,400 --> 00:04:38,600 'this', yes, in very first line you have a 'this' at very 84 00:04:38,600 --> 00:04:41,200 first. You are getting that 'this'. Then what about these two, 85 00:04:41,400 --> 00:04:44,200 eighth line and ninth line you have 'this' string, right? 86 00:04:45,600 --> 00:04:50,500 See, if you remember from our previous rules '^' - Start 87 00:04:50,500 --> 00:04:54,000 of the string and start of the line in case of multiline. 88 00:04:54,600 --> 00:04:57,600 So by default '^' will look only in the first line, 89 00:04:57,900 --> 00:05:01,000 if you have a multi-lines. But we have multi-lines and in 90 00:05:01,000 --> 00:05:03,900 multi-lines we have 'this'. If you want to look in each and 91 00:05:03,900 --> 00:05:07,200 every line, by default '^' won't look. By default '^' 92 00:05:07,200 --> 00:05:10,300 will look only in the first line, but I want to look 93 00:05:10,300 --> 00:05:13,800 in each and every line. Then you need to add a flag called 94 00:05:13,800 --> 00:05:16,600 're.m' or 're.MULTILINE'. 95 00:05:16,600 --> 00:05:19,400 Now see that. In two places at very first we have 96 00:05:19,400 --> 00:05:20,700 'this' in two lines. 97 00:05:21,300 --> 00:05:24,500 So look in all the lines, look in all multi-lines 98 00:05:24,500 --> 00:05:26,200 your Regular Expression pattern. 99 00:05:26,300 --> 00:05:28,100 That is the use of 're.m'. 100 00:05:28,100 --> 00:05:30,100 [no audio] 101 00:05:30,100 --> 00:05:34,700 Now along with your 're.m' I want to ignore case sensitivity 102 00:05:34,700 --> 00:05:37,900 as well because I need to look for this, 'This' also, 103 00:05:38,200 --> 00:05:43,900 I mean ninth line 'This' also. Capital 'T', 'h-i-s'. Then you have to 104 00:05:43,900 --> 00:05:46,200 include your case sensitivity flag as well. 105 00:05:46,200 --> 00:05:50,400 Then you have to use '|', and then you can use 're.I'. 106 00:05:50,400 --> 00:05:54,900 So if you want to work with multi-flags, you have to include 107 00:05:54,900 --> 00:05:56,900 all flags with the '|' symbol. 108 00:05:56,900 --> 00:05:59,200 [no audio] 109 00:05:59,200 --> 00:06:03,400 Right. That's it. The same way you can look for ending words as well. 110 00:06:04,100 --> 00:06:05,600 See, let me comment this. 111 00:06:07,300 --> 00:06:10,600 Let me write here, suppose this. Let me write some new thing 112 00:06:10,600 --> 00:06:15,800 'end', and here suppose I'm going to, I'm writing the 'end'. Now 113 00:06:15,800 --> 00:06:20,800 my requirement is, I want to print 'end', if 'end' is there 114 00:06:20,800 --> 00:06:26,600 at the ending in a given text or string. So pattern is, you know. 115 00:06:27,600 --> 00:06:33,100 Simply 'end', ending you're looking, '$'. Now see the result. 116 00:06:33,100 --> 00:06:35,800 [no audio] 117 00:06:35,800 --> 00:06:39,500 'end', 'end' you're getting. Why you're getting? I included 118 00:06:39,500 --> 00:06:41,900 here 're.m'. Let me remove that. 119 00:06:41,900 --> 00:06:43,700 [no audio] 120 00:06:43,700 --> 00:06:48,300 Now see that. You're not getting 'end' in the, I mean in the multi-lines 121 00:06:48,300 --> 00:06:50,500 You are going to get only in the one line. 122 00:06:51,800 --> 00:06:53,500 Sorry, ending, ending not about this. 123 00:06:53,500 --> 00:06:55,300 This is about this. See the result. 124 00:06:55,300 --> 00:06:58,100 [no audio] 125 00:06:58,100 --> 00:07:01,200 'End of the string', so in end of the string we have only 'end' 126 00:07:01,200 --> 00:07:05,100 here. But in each and every line if you want to look for your 127 00:07:05,300 --> 00:07:08,000 required string is there at the ending or not, then you need 128 00:07:08,000 --> 00:07:09,900 to include multi-line. That's it. 129 00:07:11,500 --> 00:07:13,800 At the same time if you want to include case sensitivity, 130 00:07:14,300 --> 00:07:17,800 let me write here capital 'E-n-d', or capital 'E-n-D'. 131 00:07:19,400 --> 00:07:20,400 That's it. 132 00:07:20,400 --> 00:07:23,100 [no audio] 133 00:07:23,100 --> 00:07:28,300 Okay. See guys, you can get clarity suppose if I take here and 134 00:07:28,300 --> 00:07:29,700 I am removing this multi-line. 135 00:07:29,700 --> 00:07:31,600 [no audio] 136 00:07:31,600 --> 00:07:33,900 You can keep your case sensitivity if you want. 137 00:07:35,400 --> 00:07:38,500 First let me remove multi-line. And see the result. 138 00:07:38,500 --> 00:07:41,900 You are getting only last 'end' because '$' means in 139 00:07:41,900 --> 00:07:45,900 your entire string only last line. '^' means first line 140 00:07:45,900 --> 00:07:50,700 starting, end means last line ending word. But in multi-lines 141 00:07:50,700 --> 00:07:56,600 I have 'end'. I need to find that as well. Then 're.m', and 142 00:07:56,600 --> 00:07:59,600 if you include 're.m', and if you remove 're.I' it won't work, 143 00:08:01,100 --> 00:08:02,300 because these are 144 00:08:04,600 --> 00:08:06,700 some combination with capital and small. 145 00:08:06,700 --> 00:08:09,200 Suppose if I write here small 'end', now it is going to fetch 146 00:08:09,200 --> 00:08:12,300 that because you included 're.m'. But to include this 'EnD' 147 00:08:12,300 --> 00:08:15,900 and this 'enD', you need to write case sensitivity as well. 148 00:08:15,900 --> 00:08:17,700 [no audio] 149 00:08:17,700 --> 00:08:19,300 That's it. Now see the result. 150 00:08:19,300 --> 00:08:21,800 [no audio] 151 00:08:21,800 --> 00:08:22,800 Okay. 152 00:08:23,600 --> 00:08:28,600 Okay, guys, we will see the remaining flags in later videos, okay. 153 00:08:28,700 --> 00:08:30,400 Okay guys, thank you for watching this video. 154 00:08:30,400 --> 00:08:41,025 [no audio]