Hi everyone and welcome.
My name is John and this is a CodeBerry programming school video on python.
And today were going to be looking at how to use regex.
With python now, RegEx or regular expressions is a way of matching a pattern to a string. Now we can set up the pattern to what we want and then we can use python to go through the string of text and match the words or whatever in that pattern that were after.
Now, this is a pretty big subject and theres a lot of RegEx that you can but the basics of it are quite handy.
So Im going to show you the best I can today.
So hopefully at the end of this video at least understand what RegEx is or Regular Expression and how you can use them in python.
So Im going to be using the search method.
There are quite a few other ones, but Im just going to be using re.search() for this example.
Now RegEx is built into the standard python library, but we do need to import it and to do that, we do import re Regular Expressions. What this does is it allows us access to the search command that were going to use as well as all the other ones when you get around to wanting to use them.
Now, Im going to create a string object here, and Im just going to call it string, and Im going to say heres my phone number and were going to do 555-555-555 just like this. Now, it wouldnt be uncommon or unreasonable , that we could have a lot of text around this, and we would want to specifically extract certain parts out of it using patterns and matches with RegEx So what we tend to do is we would want to use the re module here to do a search and then give it a pattern and then let it return that search object.
So Im just going to say here, Im just going say match is equal to re.search() and now we can give it the pattern that we want. Generally speaking you might have more complicated patterns in this, but the first one Im going to do is we just need to give an r" " and then we need to open our brackets and Im going to search in this text for the word phone.
Now, there would be easy ways to do this in python as well, but its important to know that this is just the basics, then we do a comma and then we give it the string to search in. Now Im going ahead and Im just going to print out this match and this will give us a match object if it finds this pattern which is just a word in this string text So when I run this, we can see down here that it says there is a match object and its a span = (10, 15).
And the match on the word phone.
Now, what this 10 and 15 is its actually the index of where the item was found in that text.
So if I close this and instead of printing out our match, I print out our string and I index it like it showed string[10:15] And now I print that, were actually going to see the word phone down here as well.
So thats where it found what it matched.
Lets go back to printing out our match.
So if we wanted to match a phone number, we could type it in like this "555-555-555".
But then were only ever going to match this specific string so thats not that useful.
But what RegEx allows us to do is it allows us to provide a pattern which will match all or any of these whatever we give it.
So Im going to say backslash d "d" Is going to mean that its going to match a digit. Now If you know how many digits you after, you can just type that in several times like this, but thats not that useful or not that handy.
But what we can do is we can give it a plus sign so thats going to match any amount of digits in a row.
So if I run this now, you might think that we get the whole of this string.
But we dont we only return the very 1st 555-555-555 because it goes along our string here, it finds all the digits and they run out and they stop at this hyphen What we can do is we can actually put that hyphen in and then we can go ahead and do our backslash d plus again.
And then a hyphen backslash, d and a plus.
Now that specific RegEx string, there is built to match any numbers that are in this format.
So if I run this, you can see down the bottom my screen, we have matched it here.
If I was to change these numbers, lets say like this, were going to be able to still match these numbers using our RegEx here.
So its going to pull it out now, you can quite easily right extremely complicated RegEx patterns to find specific parts of a string.
So, the demo Ive given you here is just on numbers, but you can do it onwards, you can match whole pieces, you can match case letters, everything else.
Its really quite a vast subject.
So hopefully you guys have got some of the basics of understanding of what it can do here and that you might want to go ahead and look into this further.
RegEx is used quite a lot in the programming world. If youre interested in more of this sort of thing definitely go ahead and checkout codeberryschool.com. Theres lots of cool stuff on that but for now Ive been John and thank you very much for watching and I will see you in the next one.