HomeОбразованиеRelated VideosMore From: Corey Schafer

Regular Expressions (Regex) Tutorial: How to Match Any Pattern of Text

5082 ratings | 156652 views
In this regular expressions (regex) tutorial, we're going to be learning how to match patterns of text. Regular expressions are extremely useful for matching common patterns of text such as email addresses, phone numbers, URLs, etc. Almost every programming language has a regular expression library, so learning regular expressions with not only help you with finding patterns in your text editors, but also you'll be able to use these programming libraries to search for patterns programmatically as well. Let's get started... The code from this video can be found at: https://github.com/CoreyMSchafer/code_snippets/tree/master/Regular-Expressions Python Regex Tutorial: https://youtu.be/K8L6KVGG-7o If you enjoy these videos and would like to support my channel, I would greatly appreciate any assistance through my Patreon account: https://www.patreon.com/coreyms Or a one-time contribution through PayPal: https://goo.gl/649HFY If you would like to see additional ways in which you can support the channel, you can check out my support page: http://coreyms.com/support/ Equipment I use and books I recommend: https://www.amazon.com/shop/coreyschafer You can find me on: My website - http://coreyms.com/ Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Google Plus - https://plus.google.com/+CoreySchafer44/posts Instagram - https://www.instagram.com/coreymschafer/
Html code for embedding videos on your blog
Text Comments (527)
dinesh19100 (2 days ago)
I need your help in splitting a log file into tokens using regular expression
Giast Sclafus (2 days ago)
Amardeep Bhowmick (3 days ago)
This is THE best explanation of Regular Expressions available in the web!!
Maciej Grabianowski (4 days ago)
Dude, this was so great, I couldn't find any easy to understand tutorial, they were all super long and used a lot of words that I wasn't sure if I understand correctly, they didn't show clear examples and I was supposed to remember that whole block of text somehow... With your tutorial I feel like I'm good to go already 💪 Thanks a lot man 🙏 Did you make that advanced tutorial as well?
Corey Schafer (4 days ago)
Thanks! And no, I still haven’t done the advanced video. I got tied up doing some other courses. But Id like to revisit this first chance I get
Ugly Sweetheart (6 days ago)
Is this applicable to VISUAL BASIC?
Ugly Sweetheart (6 days ago)
MrElectrifyer (6 days ago)
Hey Corey, have a quick question, I'm trying to match the text "V.M." in the following simple string: V.M. stands for virtual machine. With the following regular expression: (\b)(VM|V\.M\.)(\b) However, my text editor (Notepad++) keeps saying it can't find any match with that, whereas it has no problem finding a match with something like the following regular expression: (\b)(stands)(\b) Any idea why the first one isn't working?
MrElectrifyer (7 days ago)
This video should be linked to in EVERY application out there that uses regular expressions!
MrElectrifyer (7 days ago)
Thank you VERY MUCH! Was thinking I'd need to watch 3 other videos to grasp the idea of regular expression, but your single video is making me feel like a pro with regular expressions now 😃
diwaker tripathi (8 days ago)
They felt so easy. Thank you! Please make video on lookaround constructs.
Niklaes Jacobsen (8 days ago)
Wow Corey, you just blew my mind :D - now i get it ! I study software at uni, and i did not get what he tried to learn me - but you did!
the Lorde (8 days ago)
you’re a fucking genius! thx
Luis Vargas (9 days ago)
Thank you for the time and effort to make high quality education available to all of us
Jeevan Sunkersett (12 days ago)
You have it very easy. Do update your comments on this video, if you post any advanced video on RE. I want to construct an RE to parse a csv, generated from Excel (with certain column having a comma. such as address - so those will be surrounded with double-quotes), some columns could have data/ no-data/ or some rows have 10 columns filled or some rows have more (say 15)
Bloodstar123 (12 days ago)
nice example for the * quantifier!
Xristina Rose (12 days ago)
Thank you^^ is very helpful :) you explained very well
Marshal Gray (14 days ago)
It helps a lot :) Thanks for this video
Anselm Tamasang (14 days ago)
Maybe you can help... I want to match this text. "CAT: III" and also i want to match only "CAT: I" how do you do that?
Raghav Gurung (16 days ago)
This makes things so easy.. thank you
Deepal Agrawal (17 days ago)
Thank you so much :)
Shangyin Gao (17 days ago)
is it true, that always using grope can make our regular expression more understandable?
Dave (23 days ago)
I've been putting off learning regex for a while. I didn't realize how simple it was to understand.
Marina Dubini (23 days ago)
amazing tutorial, thanks!!!
MirkWoot (26 days ago)
Regex just totally messes you up, lol. But the video did help. What ind like, is a static image, with lines drawn to the regex, saying ... this is group.. this does that.. etc. I know it can be found some places.
David Gabbay (27 days ago)
greate lesson Bravo
shakattak (1 month ago)
I put off regex for so long because it never made sense to my feeble brain...until now! Thank you for making regex easy!
Shivika Panjrattan (1 month ago)
Great video!
TheDJZeroX (1 month ago)
Awesome tutorial. Whats the theme for highlighting your findings with those red brackets?
debvdo (1 month ago)
Hi Corey, I am building a personal website that tests regular expressions supplied by users. Is it ok if I embed this video in my site? Rgds.
debvdo (1 month ago)
Corey Schafer (1 month ago)
Vincent Lavello (1 month ago)
definitely a coherent and well presented tutorial!
Rahell Hamarash (1 month ago)
excellent video, very accurate, very clear. Thank you
cdm891 (1 month ago)
can anyone help me understand how to find sentence breaks? I have been trying to do this example https://regex.sketchengine.co.uk/cgi/ex4.cgi and just cant understand how to solve it
Prakash Crank (1 month ago)
I need email pattern that have 20 character before @ symbol after that 10 character
Vipul Kumar (1 month ago)
Please Suggest anyone... If I have to extract 2 table values from 6 tables. For e.g Table 1 Col1 Col2 Col3 Col4 abcd 523 aaaa 999 aber 182 aaaa 928 abqd 963 aaaa 921 Table 2 Col1 Col2 Col3 Col4 abcd 523 aaaa 999 aber 182 aaaa 928 abqd 963 aaaa 921 Table 3 Col1 Col2 Col3 Col4 abcd 523 aaaa 999 aber 182 aaaa 928 abqd 963 aaaa 921 Table 4 Col1 Col2 Col3 Col4 abcd 523 aaaa 999 aber 182 aaaa 928 abqd 963 aaaa 921 ............................................................................................. I need only table 2 & 3 values. (start from Table 2 end Table -3) Thanks in advance
Michael Gurkov (1 month ago)
Thank you! Great video
Sam Saleh (1 month ago)
How can I utilize Regular Expression to Group customer names in a a SQL table, where customer names are listed more than 1000 times in some cases and in many cases spelled differently? Thanks in advance.
Stefan Ivovic (1 month ago)
one of the best regex practical explanations
roksana rezaei (1 month ago)
How can I watch your next Regular Expression Tutorial?
Corey Schafer (1 month ago)
Yes, they will be posted to my channel when they are complete. I post all of my content here on YouTube
roksana rezaei (1 month ago)
+Corey Schafer thanks for your response! I am actually learning Python. I have subscribed your channel. Are you going to post them there?
Corey Schafer (1 month ago)
I haven't got around to filming it yet. I would like to get around to that soon, but I have a few more Python videos I plan to release first.
Mahdi Ali (1 month ago)
Awesome! I appreciate it, thanks, Man.
Mostafa Lotfi (1 month ago)
Thanks for this video Corey. I have it bookmarked and keep referencing back to it. It's super easy to understand and I appreciate you producing this content. Would it be possible for you to also link to, or just outright add to the description notes, that regex cheat sheet you have open on the side panel of Atom. It's super useful and I find that I keep scrolling to parts of the video just to catch a glimpse of it here and there.
Mostafa Lotfi (1 month ago)
+Corey Schafer Awesome thank you so much!
Corey Schafer (1 month ago)
Hey there. I have those code snippets linked in the description section. Here is a link specifically to that cheat sheet: https://github.com/CoreyMSchafer/code_snippets/blob/master/Regular-Expressions/snippets.txt
moose43h (1 month ago)
mr t lol
Çağrı K. (1 month ago)
thank you! great explanation
Raman Rajagopal (1 month ago)
Nice One. Thanks for posting the video.
yukiyanagi311 (1 month ago)
Hi, Corey! Thank you for such a nice video. Every time I scrape web pages, I wa suffering to regular expressions orz But I understood well thanks to your video.
Laxman Pandey (1 month ago)
Detailed and simple way to learn regex and now its not wiered to me :) Thanks for the video
Jithesh Balan (1 month ago)
Thanks buddy!
Tiffany Wismer (1 month ago)
This is amazing. You made regex so simple.
Paul Cristo (1 month ago)
Very informative video. Thanks.
Berny Baeza (1 month ago)
Thanks, very clear
great video
zishan shaikh (2 months ago)
Very helpful tutorials... thank you
Ike Egbun (2 months ago)
I have come to watch this video several times, and its my go to video for regex refresher. Kudos Corey
Vincent Setlale (2 months ago)
Thank you
Code Ninja (2 months ago)
i didn't wanted to even look into regex, but than i found your video and i was sure already that it worth watching.
Amr K. Ali (2 months ago)
best regex video I've seen so far. you should do it on udemy
Brahm Aggarwal (2 months ago)
You are the best.
Shawn Dhave (2 months ago)
Really wish someone as good as you could explain this in a java tutorial. Bravo mate!!!
Nikhil Adiga (2 months ago)
Thanks man!
Bharat Shinde (2 months ago)
Thank you for the video . It helped me understand regular expressions easily. Thank you
rashmi gunda (2 months ago)
Very good explaination...please upload video having regexp switches, such as -all,inline etc
equinoxenomorfo (2 months ago)
Great video, great commentary.
Eric Lupien (2 months ago)
Excellent video. You've explained regular expressions really well. Thank you!
davesun2000 (2 months ago)
Very well done. Thanks!
Imran Naseem (2 months ago)
Great video. made RE for me look easier. thank you sir
Pavankalyan Panga (2 months ago)
Good work
Hokage Espada (2 months ago)
Thanks for the information this is very helpful
Turar Jumaniyaz (2 months ago)
Hi Corey! I've encountered a problem with the word boundary. I copied your regex into my Atom's search bar ('\bHa') and had in my text file text 'Ha HaHa'. However, when I used that regex, it returned to me 3 results (so all the Ha's). Could you please tell me what could be wrong here?
Ben De Loyola (2 months ago)
hi sir, i appreciate your video, it really easy to learn on your video, but i have a question, how about the 3rd example in your email.txt, it has a 3 dash, but you only put a 2 dash. is it not separated the first word and the numbers? because in your regex you combined it as well.
Ben De Loyola (2 months ago)
+Corey Schafer oh okay sir thank you sir
Corey Schafer (2 months ago)
Hey there. That is because with the '+' looks for one or more of those characters. For example, there are multiple of the same letters as well, but it goes over all of those until it hits the @ symbol. Hope that makes sense.
Gnan Helboy (2 months ago)
what a wonderful demo on regular expressions. i am so happy because i don't feel regular expressions are difficult anymore.
Dhoomketu (2 months ago)
Dude you are GOD sent.
Rafael Paz (2 months ago)
Outstanding explaining mate! Thank you so much!
Leon (2 months ago)
This was my first video on learning regular expressions and began to think Regex isn't that hard at all, then reading the comments its apparent thats its actual you whose good at teach. Great tutorial. Thanks!
Corey Schafer (2 months ago)
Well Explained. Thank You
Jovan Djordjevic (2 months ago)
Thanks, really helped, keep going!
The Dude (2 months ago)
please make an advanced video on regex!
Meera Anant (2 months ago)
Fantastic tutorial. Thank you. Looking forward to another one on : How to Delete and Replace
Bisikletli Abi (2 months ago)
Thank you
Andres Vodopivec (2 months ago)
Whenever I see talented people like you giving perfect explanations I feel confident to say: things are not difficult, just badly explain. Thanks to you I was able to FINALLY understand regex and actually start looking at it very friendly. THANKS so much!
umar farooq (2 months ago)
RE of string having length even but no of a's are odd
Ramgopal Panda (2 months ago)
Thank You man, Thanks a lot, I cant express my felling, Waiting for your Advanced concepts.
Aditya Tyagi (2 months ago)
Can you explain the difference between groups () and Character set[] . Both of them seems similar to me
Zandrea Eslick (2 months ago)
great tutorial!
Zaid Al Dhahi (2 months ago)
Man!! You're really awesome. Thank you very much.
limeonion (2 months ago)
This makes me feel smart.
Pravin Pandirkar (3 months ago)
@corey: Could you let me know how you used the bottom screen to run the code when you were matching the pattern
Pravin Pandirkar (3 months ago)
@corey: I figured out
lahiru udana (3 months ago)
Thank you !
James Donlon (3 months ago)
EXACTLY what I was looking for. Great explanation, thank you!
Joe Elyahchouchi (3 months ago)
I think that was a forward slash what you were using in the beginning.
Manjunatha M (3 months ago)
100% worth watching, you got awesome teaching skills man
Manjunatha M (3 months ago)
Hello Corey, would be great if you can make an another video of regex, which includes some complicated search and grouping. Would love to watch that
Wolf Films (3 months ago)
I do not think that the expression at 11:04 example is correct. It would match any number with or without full stops and dashes. If you were to use the expression on this 123456789131, it would apply to this too. When it should not!
Aamir Jamal (3 months ago)
Thanks bro.
sakthi balan (3 months ago)
you made it simple. Thanks
9981973710 (3 months ago)
38 mins spent and ya which spent well without any bore. Thanks Corey Schafer.
Seung-Un Ham (3 months ago)
First time learning regular expression. Useful and easy to understand.
Veronica Indimuli (3 months ago)
Time well spent....making Regex easier..for once i enjoyed watching..thank you,=.
Vikas NS (3 months ago)
Giuseppe Guerini (3 months ago)
This is a regular expression I made for email addresses: [\w.-][email protected][a-z]+\.[a-z]{2,3}
ViralGrenade (3 months ago)
Dude... this is amazing. Saved my ass.
Victor Cardona (3 months ago)
Can you do a more advanced tutorial? You are awesome!

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.