1 00:00:00,000 --> 00:00:01,900 [no audio] 2 00:00:01,900 --> 00:00:03,200 Friends, here we are discussing 3 00:00:03,200 --> 00:00:06,800 about 'split', 'sub', 'subn' operations 4 00:00:06,800 --> 00:00:08,199 of your 're' module. 5 00:00:08,400 --> 00:00:13,000 Let me open my Python terminal, and there I will explain. See 6 00:00:13,000 --> 00:00:15,700 anyway they are very simple operations compared to your 7 00:00:15,700 --> 00:00:19,900 'search', 'match', 'findall', 'finditer'. 8 00:00:20,300 --> 00:00:25,000 Let me open my python terminal, and let me import 're' module. 9 00:00:25,200 --> 00:00:26,900 So first operation is suppose here 10 00:00:26,900 --> 00:00:30,000 we want to discuss about 'split' operation, and if you remember 11 00:00:30,000 --> 00:00:32,500 while starting our Regular Expressions we have started 12 00:00:32,500 --> 00:00:35,200 with 'split' operation only, I mean while giving introduction. 13 00:00:35,700 --> 00:00:38,400 Anyway, here I want to show you some advanced syntax of your 14 00:00:38,400 --> 00:00:39,400 'split' operation. 15 00:00:39,600 --> 00:00:43,700 So simply write 'help(re.split)' operation, and see the 16 00:00:43,900 --> 00:00:47,500 result. We are getting some syntax, right. Now, first 17 00:00:47,500 --> 00:00:48,900 let me take 'my_srt' as, 18 00:00:49,800 --> 00:00:55,500 so, "This is about python and python is very easy 19 00:00:55,500 --> 00:00:57,400 [no audio] 20 00:00:57,400 --> 00:00:58,600 and we are having 21 00:00:58,600 --> 00:01:01,600 [no audio] 22 00:01:01,600 --> 00:01:08,800 python2 vers and python3 vers". Just assume that 23 00:01:08,800 --> 00:01:13,000 that is the string. Now suppose I want to split the given string, 24 00:01:13,000 --> 00:01:15,200 directly I'm splitting. So here is itself 25 00:01:15,200 --> 00:01:16,200 I am writing a pattern. 26 00:01:16,800 --> 00:01:18,600 Otherwise, you can also write your pattern, 27 00:01:18,600 --> 00:01:20,400 let's say, 'my_pat' as, 28 00:01:20,400 --> 00:01:22,200 [no audio] 29 00:01:22,200 --> 00:01:24,100 so I want to divide my string wherever 30 00:01:24,100 --> 00:01:25,200 'python' is there. 31 00:01:26,100 --> 00:01:30,900 Right. Now simply use 're.split' operation, your pattern, 32 00:01:31,200 --> 00:01:33,800 and your string, now see the result first. 33 00:01:35,300 --> 00:01:37,000 See wherever 'python' is there, 34 00:01:37,000 --> 00:01:38,400 there split is happening. 35 00:01:38,900 --> 00:01:40,800 But here this is capital 'P', 'Python'. 36 00:01:41,400 --> 00:01:43,500 But if you observe here this is not simply 'python', 37 00:01:43,500 --> 00:01:44,900 'python2', and 'python3'. 38 00:01:45,400 --> 00:01:52,400 Now, what I want to do is, wherever 'python2', or 'python3', and 39 00:01:52,400 --> 00:01:56,100 with 'python', then there I want to do a split. Now, 40 00:01:56,100 --> 00:01:58,500 if you observe your pattern is going to represent 'python', 41 00:01:58,600 --> 00:02:01,800 'python2' and 'python3', but of course here you are having 42 00:02:01,800 --> 00:02:06,300 capital 'P', so that you can ignore by using, by applying flags. 43 00:02:06,400 --> 00:02:09,100 Now see that, I am applying a 'flag', 44 00:02:09,100 --> 00:02:11,800 [no audio] 45 00:02:11,800 --> 00:02:16,000 case sensitivity, 'flag=', don't consider case-sensitivity, 46 00:02:16,000 --> 00:02:19,100 ignore case-sensitivity. Now see the result. Wherever 'python' is 47 00:02:19,100 --> 00:02:22,100 there, or 'python2' is there, or 'python3' is there, there 48 00:02:22,100 --> 00:02:23,100 split is happening. 49 00:02:23,700 --> 00:02:27,600 Right. So the only thing is, you have to know how to write a pattern, 50 00:02:28,400 --> 00:02:31,400 where you want to split, in which places you want to split, 51 00:02:31,400 --> 00:02:33,100 based on that places 52 00:02:33,300 --> 00:02:36,100 you need to write a pattern. Already we have gone through the 53 00:02:36,100 --> 00:02:37,600 rules to create your pattern. 54 00:02:38,300 --> 00:02:40,700 And one more thing, here if you observe here, 'maxsplit' is 55 00:02:40,700 --> 00:02:42,500 also there, right. Now, 56 00:02:42,500 --> 00:02:46,400 let me run this with 'maxsplit' as well. 57 00:02:46,400 --> 00:02:48,800 [no audio] 58 00:02:48,800 --> 00:02:51,200 Let me copy this. Directly you can write. First 59 00:02:51,200 --> 00:02:54,100 I'm applying 'maxsplit=0', and see the result. 60 00:02:54,100 --> 00:02:58,300 There is no change whether if you write 0, or I mean 'maxsplit=0', 61 00:02:58,300 --> 00:03:00,700 or if you neglect this option there is no change. 62 00:03:00,700 --> 00:03:05,300 But if I write 1, 'maxsplit', split only in one place, that 63 00:03:05,300 --> 00:03:09,500 too from left to right, the first occurred place. Now split 64 00:03:09,500 --> 00:03:13,500 in two places, that too while coming from left to right, 65 00:03:13,500 --> 00:03:15,500 this is left to right, right, left to right. 66 00:03:16,400 --> 00:03:19,500 So they're divided in two places. Now see the result. Here 67 00:03:19,500 --> 00:03:21,000 and here it is splitting. Now 68 00:03:21,000 --> 00:03:22,000 here and here 69 00:03:22,000 --> 00:03:23,000 it is not splitting. 70 00:03:23,400 --> 00:03:27,600 So that is the simple operation by using your 'split' operation. 71 00:03:28,700 --> 00:03:32,100 Right. Then we have 'sub'. Now, 72 00:03:32,300 --> 00:03:36,400 let me show you syntax for your 'sub' first. See the 73 00:03:36,400 --> 00:03:41,300 result. Pattern, I mean whatever the pattern you want, in this 74 00:03:41,300 --> 00:03:44,200 pattern whatever the word you want to replace, then your 75 00:03:44,200 --> 00:03:46,300 string, then count, then flags. 76 00:03:46,400 --> 00:03:48,000 Anyway, you know the usage of flags. 77 00:03:48,800 --> 00:03:49,800 Let me write simply, 78 00:03:50,400 --> 00:03:51,900 anyway you have your pattern, right. 79 00:03:52,000 --> 00:03:53,000 What is your pattern? 80 00:03:53,000 --> 00:03:56,000 [no audio] 81 00:03:56,000 --> 00:03:57,900 Let me write simply, your pattern. 82 00:03:58,200 --> 00:04:00,100 So 'python [23]', right. Now, 83 00:04:00,100 --> 00:04:06,400 what I am doing is, simply 'print(re.sub, in place 84 00:04:06,400 --> 00:04:09,800 of your patterns - 'python', or 'python2', or 'python3', 85 00:04:10,100 --> 00:04:12,500 I want to replace with suppose 'jython', 86 00:04:14,100 --> 00:04:15,200 in your given string. 87 00:04:16,300 --> 00:04:17,300 See the result. 88 00:04:17,300 --> 00:04:19,300 [no audio] 89 00:04:19,300 --> 00:04:21,899 Wherever 'python' is there, there we are replacing. 90 00:04:23,000 --> 00:04:25,800 But here it is not replacing because it is capital 'P'. Then 91 00:04:25,800 --> 00:04:29,000 apply 'flags' to ignore your case sensitivity. 92 00:04:28,800 --> 00:04:31,800 [no audio] 93 00:04:31,800 --> 00:04:35,600 Now, all places it is replacing. But the thing is, it is not 94 00:04:35,600 --> 00:04:38,400 going to disturb your original string. Be clear. You know 95 00:04:38,400 --> 00:04:39,800 the string properties. 96 00:04:41,000 --> 00:04:43,800 So in that case, in case if you want to store the result, 97 00:04:43,900 --> 00:04:47,000 then you can take 'my_new_str', 98 00:04:47,500 --> 00:04:50,100 I mean you can take any variable, and in that you can store it. 99 00:04:51,400 --> 00:04:53,100 Now just print your 100 00:04:56,100 --> 00:04:58,800 new string and see the result. That's it. 101 00:05:00,100 --> 00:05:04,500 Right. And if you observe here, 'count', 'count' means in how many 102 00:05:04,500 --> 00:05:06,000 places you want to replace. 103 00:05:06,400 --> 00:05:11,800 Let me directly print by applying your 'count'. 'count=0', 104 00:05:12,000 --> 00:05:15,000 see the output first. There is no change whenever if you write 105 00:05:15,000 --> 00:05:19,100 'count=0', or even if you skip that option 'count=0'. 106 00:05:20,400 --> 00:05:25,200 But if you write 'count=1', see, only the first place 107 00:05:25,200 --> 00:05:28,600 where you are occurring your pattern, there it is replacing with 'jython' 108 00:05:29,500 --> 00:05:34,400 That's it. But if you write 2, see whenever if you write 2, in 2 places 109 00:05:34,400 --> 00:05:38,100 replace my pattern with required string. This is your pattern, 110 00:05:38,100 --> 00:05:39,400 and this is your required string, 111 00:05:40,600 --> 00:05:44,000 and replace in two places only, that too while coming 112 00:05:44,000 --> 00:05:45,900 from left to right. That is the preference. 113 00:05:47,000 --> 00:05:49,000 See here and here it is replacing. That's it. 114 00:05:50,000 --> 00:05:52,800 Then what about you are 'subn' operation. 115 00:05:52,800 --> 00:05:53,900 You have one more operation, right? 116 00:05:53,900 --> 00:05:55,200 Nothing is there. It's very simple. 117 00:05:55,200 --> 00:05:56,800 Just let me run, and see the result. 118 00:05:58,100 --> 00:06:02,300 It is going to show you your result as a tuple, 119 00:06:03,700 --> 00:06:08,600 and it is giving you a resultant string, and here you are getting 120 00:06:08,700 --> 00:06:12,900 a value called 2 means, in 2 places you are replacing. Anyway, 121 00:06:13,300 --> 00:06:15,700 manually you are mentioning in 2 places replace, but you don't 122 00:06:15,700 --> 00:06:18,400 know in how many places you have your required pattern 123 00:06:18,400 --> 00:06:21,600 in a given string. Just observe that I am removing 'count=2' 124 00:06:23,100 --> 00:06:25,700 Now wherever your pattern is there, in all places you are 125 00:06:25,700 --> 00:06:26,800 going to replace with 'jython'. 126 00:06:26,800 --> 00:06:29,400 But how many places you have your required pattern in a given 127 00:06:29,400 --> 00:06:33,300 string, that it will give. Now see the result, in four places 128 00:06:33,300 --> 00:06:36,200 actually you have your required pattern in a given string, and it 129 00:06:36,200 --> 00:06:38,900 is replaced in four places with 'jython'. 130 00:06:39,100 --> 00:06:40,800 That's it. That's what it is giving. 131 00:06:41,400 --> 00:06:44,100 So this value is not depending on your 'count' value. 132 00:06:44,500 --> 00:06:47,300 But if you write 'count=2', it will replace with only 133 00:06:47,300 --> 00:06:50,300 two, that means you're going to get value 2. But if you don't 134 00:06:50,300 --> 00:06:54,800 use 'count' option in your 'sub', then in all places it 135 00:06:54,800 --> 00:06:57,800 is going to replace, but how many places it has been replaced, 136 00:06:57,800 --> 00:07:02,300 that value will be given by using 'subn' operation 137 00:07:02,300 --> 00:07:03,500 of your 're' module. 138 00:07:04,300 --> 00:07:05,900 That's it. So Guys, 139 00:07:05,900 --> 00:07:08,800 these are the simple ways to use your 'split', 'sub', and 140 00:07:08,800 --> 00:07:10,800 'subn' operations of 're' module. 141 00:07:11,300 --> 00:07:14,000 Very, very useful. Please try to do practice on this. 142 00:07:14,700 --> 00:07:16,400 Okay guys, thank you for watching this video. 143 00:07:16,400 --> 00:07:23,105 [no audio]