Coding 101 23 (Transcript)


Download and watch the episode here:
Coding 101 23

Shannon Morse: Today on Coding 101 it’s all about regular expressions. Like happy. And sad. And angry. Stay tuned.

Netcasts you love from people you trust. This is TWIT. Bandwidth for Coding 101 is provided by CacheFly. At C-A-C-H-E-F-L-Y .com. This episode of Coding 101 is brought to you by Squarespace. The all in one platform that makes it fast and easy to create your own professional website or online portfolio. For a free two week trial and 10% off, go to squarespace.com and use the offer code CODING. And by Hover.com. Hover is the best place to buy and manage domain names. It’s simple, honest and easy to use. For 10% off your first purchase go to hover.com and enter the promo code C101.

Fr. Robert Ballecer: Welcome to Coding 101, it’s the twit show where we let you into the wonderful world of the code monkey. I’m Father Robert Ballecer.

Shannon: And I’m Shannon Morse. And for the next 30 minutes we are going to tell you all about everything you need to know to get all coded up and be a code warrior in the world of Perl.

Fr. Robert: Now before we get to that Shannon, there’s a couple of house cleaning items we have to take care of. Because we’ve actually been gone for a while.

Shannon: We have, yeah. I had to do this marriage thing.

Fr. Robert: Yeah, you got married. I mean, whatever. We have a show to do, fine if you want to take a day off for getting married, whatever.

Shannon: Work smirk.

Fr. Robert: Well but we did do the first two episodes of the Perl module back to back. And as a result I have a mia copla here. Which was my show notes, which I thought I had wonderfully packed up before I went away for two weeks of vacation and work, turns out they didn’t work out.

Shannon: Yeah, I went to the Twit site and I was like, where are the show notes?

Fr. Robert: Yeah, so that’s my bad. I’m sorry about that folks. I will get that fixed. And actually this is a good time for me to reconfigure it. There are a few people in our google+ community who are trying to move us to a better way to use GitHub. Using something like MarkUp. And some of it looks really, really promising. So I’m going to toy around with it for the next week or so and see if there’s not a better way for us to put our notes and our code into one place. Because right now it’s kind of separate.

Shannon: Yes it is. We have our show notes over on twit.tv and we have our actual code over on GitHub. But I didn’t think you could use GitHub for more than just uploading the files.

Fr. Robert: You’re not supposed to. But evidently some people have figured it out. And it looks good so I will be jumping around. I will be trying a few things. I’m not going to do it immediately because I don’t want to switch to a new system and then find out that that system won’t do everything we want to do. But I think we can all agree that the way that we’re doing it right now is a little cumbersome especially for first time coders. So we’re going to try to find a better way for you to find all of our code and all of our notes in one simple spot.

Shannon: Yay, I think that sounds great.

Fr. Robert: There is something else that we wanted to do. You know one of the thing that we pride ourselves on is our community right?

Shannon: Oh, yeah. I love our community.

Fr. Robert: It’s a big part of who we are, it’s a big part of what we do. We try to drive people to our G+ community because it’s filled with really bright engineers, with really bright programmers and it’s just a good place to hang out if you want to learn the crap. Well there are some people in our community who are very vocal. And they would like a go at teaching a 101 level course. And you know what, I’m not one to say no. so here’s what we’re going to do. The first one who approached me was a man who is in the community by the name of Jo Heck. He’s been with us since the very beginning. Code4Sale I believe is the moniker that he uses. But he’s an expert programmer and what I want to do is I want to get him two episodes on a Friday. To be able to do the show the way he wants. This is not a challenge folks, this is not “oh yeah? Well put up or shut up”. I honest to goodness want him to be able to do the best job that he can and maybe we’ll take some pointers on how we guide Coding 101.

Shannon: I think it’s a good idea.

Fr. Robert: Yeah. Give us some time because …

Shannon: When I was on Before You Buy I had people come on and do guest reviews.

Fr. Robert: Exactly, yeah that’s how it works, yeah. I mean, no one’s ever got the market on how you teach so we want to hear new ideas. Now it’s going to take us awhile to get the setup because we got to get the logistics worked out because it’s not a standard Coding 101 episode so we have to make sure we have time and I have to make sure we have a format down because obviously you work most of the time away from twit. So stay with us, we will figure this out and we will make sure that we include more of our audience because that’s who we are right? We’re nothing without our audience.

Shannon: Speaking of which, we do have some guest feedback, some guest programming codes. I’m really excited about these because it’s been so many weeks since I’ve actually gotten to play with them. So we’ve got a couple really cool ones in our Google+ community of course. If you haven’t checked that out its over at Gplus.to/twitcoding101.

Fr. Robert: Wow. Gone for three weeks and still pulls them right off the top of your- I love that.

Shannon: But I wanted to go ahead and start off with a very simple code that I decided to do to just review what we’ve learned so far. Now obviously there is tons that you can learn from Perl episode 1 and Perl episode 2 that we’ve already done in our module. But if you’re just catching up I can go ahead and show you what I’ve done so far. So let me go ahead and pull up this code on my computer. And if you want to pull up my computer please. Thank you sir. So if I just click on this it’s going to ask me who my favorite Dr. Who companion is. My personal favorite is Amy. So I’m going to type in Amy.

Fr. Robert: Now those are fighting words.

Shannon: Uh-oh. Oh snap, “You said Amy. I agree. Amy rocks.”

Fr. Robert: Gee, I wonder who programmed that.

Shannon: of course if you do come back in here and say your favorite ends up being Rose, it says “meh, they were okay… I guess. But the best was obviously Amy”.  And I say they because there were some males. And then just hit enter to close. Now if I go over here to edit to actually view the code. Ill pull this up so you can see all of it. I did include comments this time. I know some people wanted to see comments, so I did include those.  Not very many but it’s a pretty simple code. So you basically just print out at the beginning who’s your favorite Dr. Who companion, \n to enter into a new line. Companion equals standard input. So this is the users input. We’re going to call it companion, whatever they end up inputting into the programming code. Now if the companion equals Amy, or Amelia, so down here it says if the companion is equal to or has Amy included in their input, then it’ll say “You said Amy. I agree, Amy rocks.” Same thing for Amelia. And last week we did learn that G means global. So it’s going to look at all of their input and look for specifically A-M-Y in line. And then the I basically means it doesn’t care if it’s uppercase or lowercase.

Fr. Robert: Right.

Shannon: So I could put this all in upper caps like that, it doesn’t matter. It’ll still read it out “I agree. She rocks.”

Fr. Robert: Now I’m glad that you pointed that out. What we should point out the fact that those, the little qualifiers that you can add to the end of regular expression formuli, are incredibly important. Because that’s one of the things that makes regx so powerful in Perl. Because it lets you search through as you’re doing here. Any string, any piece of data, any file. Looking for particular thing that you want to match or replace. And that’s what you’re doing.

Shannon: Exactly. So that was the if part of my command. I also have the else statement down here. If you do not put in Amy or Amelia, in your standard input, than it’ll just say “you said (whoever you actually did put in)” and then “meh… they’re okay, I guess. But the best was obviously Amy.” And then an enter. And then at the very end it just waits for the user to hit the enter key to close it out.

Fr. Robert: So dismissive. And remember there are other ways to make it wait than using standard input.

Shannon: Yes, there are.

Fr. Robert: That’s the way we’re doing it, if you use another method, that’s fine. Go ahead. It’s not going to hurt our feelings. But standard is actually my favorite.

Shannon: It’s a good one, yeah. It’s easy and it’s the one that I learned so I’m going to use it. Now we also got two more in our Google+ community, again, gplus.to/twitcoding101, go subscribe because its, super fun. Now the first one is from Jerry Coffey and he said this is a simple math trick that he decided to do in Perl. And it totally works and it kind of freaked me out but I’m going to show you it anyway. Alright. Jerry math trick. So think of a number. So I’m going to think of…

Fr. Robert: I hate these. I really do.

Shannon: I know. So I’m going to be thinking of 8. “Press here when you’re ready.” “Multiply that number by 2.” So it would be 16. “Press enter.” “Add 10 to that number.” So it would be 26. “Divide that by 2.” So that would be 13. “Subtract your original from the last.” So that would be 13-8 and that equals what, 5? 5. Then it says “wait while I read your mind.”

Fr. Robert: And he’s a witch.

Shannon: “The answer is 5!”

Fr. Robert: Now of course this is just math, this is just having fun with math. We always remember. But it’s kind of fun to see that in code.

Shannon: It is, it’s super, super cute and it’s a pretty easy code to do too. It’s basically just taking a bunch of standard inputs, which are your enters, and then giving you different output each time at the very end, here, we see that he sleeps it for one second. So that’s something new that we haven’t stepped on yet.

Fr. Robert: Mm-hmm.

Shannon: its sleep for one. And you could also obviously change that to 2, 3 or whatever you wanted to do. So it doesn’t do anything for one second. Pretty cool entry right there. And the answer is 5!

Fr. Robert: It also, you know, the ability to pause is sort of the start of animation. In a programming language. Any time you can pause, you can add motion. It’s a clunky way to do it but I’ve seen people do some awesome things with pausing in asking animation so…

Shannon: I like it. I think it’s pretty neat. Our second one, and our last one for today, is from Darryl Medley. And this is an animal farm sounds program. So if I open up this one, click on the farm. Okay, so first off I’m going to list all the animals on Darryl’s farm. So I click list and it shows you all the animals he has on his farm. He has some good internets for his farm. I’m going to say “sheep”. So I enter sheep and then it says the sheep says “baa, baa”.  That’s it. Cow. “Cow says moo”.

Fr. Robert: Are there any snubs on this farm?

Shannon: “oink, oink.” Snubs. Sorry.

Fr. Robert: Aww, so sad.

Shannon: It doesn’t have a snub. And then you can just leave it blank and hit enter for it to exit. That’s it.

Fr. Robert: Yeah. Now this seems really simple but some people were freaking out, and rightfully so. Over the regular expressions. So we’re going to spend a lot of time this episode talking about regular expression. In fact, one of the things I think was a mistake on my part was we didn’t give enough time to our code warrior to really go through regular expression. Give you all the different ways you can use it. And explain why it’s so important. So we’re going to- looking forward to that in just a bit. But before we get to that module I wanted to do one more thing that came to us from our community.

Shannon: Uh-oh. Oh boy. What’s going on?

Fr. Robert: It’s the lighter side of programming.

Shannon: Oh, the lighter side, this is new!

Fr. Robert: So Collin Nelson brought this to our attention in our G+ community for Coding 101 and I don’t know if you’ve got that link up Brian, but he wanted us to take a look at a language that was created by some geeks. Obviously some very bored geeks who created a new programming language based entirely off of Arnold Schwarzenegger’s catch phrases.

Shannon: Oh my gosh.

Fr. Robert: Now, for no reason whatsoever they have created ArnoldC.  That allows you to call functions and methods and procedures with commands like “listen to me very carefully”. Which declares methods, or “get your ass to mars” which assigns variables from methods. Now if you go to the actual code itself, to the GitHub, even “hello world” is interesting. It uses for print, “talk to the hand”. And of course every program ends with “you have been terminated”. So… no reason to create something like this, but it just shows that yeah, programmers have a sense of humor too.

Shannon: Oh. Programmers are weird.

Fr. Robert: Okay. So programming experts, if you could make us a Harry Potter language that would be awesome.

Shannon: Wait, is there not already a Harry Potter language?

Fr. Robert: Spells! How awesome would it be to have spells?

Shannon: Oh, that’s a great idea!

Fr. Robert: Seriously, right?

Shannon: Yes! I like it.

Fr. Robert: Okay, that was a horrible idea. It would be fun. I would do it.

Shannon: Aw, I like it. I don’t remember any…

Fr. Robert: It just reminds me of my college days and there was always that one geek who hated all of the compilers so he wrote his own.

Shannon: Expecto Patronum!

Fr. Robert: It’s like, why would you do that?

Shannon: Well, you know, a site is online and it’s available to you, that enables you to not have to learn code.

Fr. Robert: Yeah. And I would like that. Because sometimes you just want to use what’s there, sometimes you just want easy.

Shannon: Yeah, sometimes you just want easy and luckily we have a site called Squarespace. Over at squarespace.com and it’s the one stop platform for everything that you ever need for all of your wonderful programming, websites and all those needs. So squarespace is the number one platform that makes it easy to create or build your own professional website or online portfolio. Some of the reasons why you’re going to love squarespace, because I know you will, they’re constantly improving their platform. With new features, new designs, and even better support. And they have beautiful designs. There are over 25 beautiful templates for you to quickly build your own personal and business site. And they also have a really good mobile experience too. So say you’re on the go and you want to check out your website or check out a friend’s website and they are using squarespace, they’ve developed templates with responsive designs. So they’ll know if you’re on your smart phone, your tablet, your computer, and they’ll be able to make it look pretty for you and its super easy like Padre said. So if you want some help squarespace has a live chat and email support 24 hours a day, 7 days a week. Plus there’s a completely redesigned customer help site for easier access to self-help articles and video workshops. Guess what? Your hosting is included.

Fr. Robert: No!

Shannon: Yeah! You don’t have to go to a second site and sign up for hosting, its right there on squarespace. They take care of the hosting. They have a distributer network that does not go down too. So you’re always up no matter what. Even when they had some crazy stuff going on in New York and like, their basement flooded, they take care of you.

Fr. Robert: I love that story. The bucket brigade of fuel.

Shannon: I remember that, my site was still up and I was like YES! And not to mention, it’s really inexpensive. I’m not going to say cheap, because cheap sounds bad. It’s inexpensive, you know, its good quality and it’s inexpensive. Its only 8 bucks a month. And that includes a free domain name if you sign up for a year. So definitely sign up for a year. It’s a good choice, a good value. And their code is gorgeous. They take so much pride in their back end code. Just as much as they do in the front end. So you’re going to get an excellent design and excellent professional quality whether you’re a coder or you just used to work on terrible sites when you were in high school.

Fr. Robert: Just some guy. Just some dude. Yeah.

Shannon: Like me. And they also have a developer platform if you really want to dig into site customization and developer accounts that never expire while in development. So you can take as much time as you need before launching your site and you can pay nothing until you actually go live. Their developer platform features include complete control. So you can edit all the code that affects the display of your website, every line of it. HTML, CSS, JavaScript, it’s all under your control. And they also have a repository for GIT IN SFTP with version control that comes standard, you can connect to template files via Git or SFTP and developer tools so there’s less preprocessing, there’s jaslon temploading, script comboing, ready responsive image handling and so much more. So you’re pretty much getting, like, the whole shebang on squarespace, so you’re good to go. So start a free 2 week trial with no credit card. No credit card required. And start building your website. When you decide to sign up for squarespace make sure to use the offer code CODING. That’s C-O-D-I-N-G. Get 10% off and show your support of Coding 101. Of course we thank squarespace for their support of Coding 101 and remember, a better web awaits. And it all starts with your new squarespace website. Thank you squarespace, I love my site and you guys do an awesome job.

Fr. Robert: We love you squarespace, thank you.

Shannon: We love them. I put my wedding website on squarespace.

Fr. Robert: What? Really?

Shannon: Yep, I did.

Fr. Robert: Wait, how come you haven’t sent a link.

Shannon: I didn’t share it because it had all the details on there so. I didn’t want crazy people showing up.

Fr. Robert: OH I guess. Yeah. No, there are no crazy people on the internet.

Shannon: Yeah. I did it all on squarespace, it’s super easy. No crazies.

Fr. Robert: Yeah, okay. Let’s get past the crazies and for a second let’s talk about the regular expression. Now I know that in episode 2 of the Perl module we kind of glossed over regular expression. I’m really sorry. The feedback that we have gotten is that we should not have done that. My bad. I thought regular expression was sort of a no brainer. It was something that was going to be simple. Obviously we should have spent more time on it and that’s what we’re going to do this episode. Now Snubs, we’ve done expressions, you’ve seen expressions, like whenever we’re talking about conditions like, if then loops. If then statements for loops. We’re talking about an expression that’s being used in some sort of comparison right?

Shannon: Right.

Fr. Robert: For example, if x is equal to y, or if y is less than 2. Those could be considered expressions but we’re using a conditional operator to find out what kind of action we want to take with that expression.

Shannon: Right.

Fr. Robert: A regular expression is really not that much different, that’s why we thought people would get it right away. It’s looking at a string, and trying to find patterns.

Shannon: Okay.

Fr. Robert: Let’s break that down. Just a tiny bit. So when we talk about a regular expression or a regx, or a regx, depending on how you want to call it. But when you’re talking it’s probably easier to call it a regular expression because no one will get mad at your pronunciation. It’s a type of pattern matching that can be used in searches or find and replace operations.

Shannon: OH. Interesting.

Fr. Robert: Yeah. If we break that down even farther, in a regular expression each character in a string becomes its own meta-character. Right. And you can combine meta-characters in order to create something that will be searched for within any piece of data. It can be a single string, it could be an entire file. It could be the entire internet. That’s one of the nice things about using a regular expression. Yeah, it’s weird. But let me explain why that works.

Shannon: Okay.

Fr. Robert: Regular expressions can be used in most modern languages. Now, Perl is sort of like the standard bearer. The Perl type of regular expressions, used in Perl is also used in Java. It’s used in JavaScript, it’s used in Rumi, it’s used in .net.

Shannon: Really? Wow.

Fr. Robert: Yeah.so if you learn how to do it in Perl, you’re going to understand regular expressions in all of those other languages.

Shannon: Oh, that’s awesome. That could be very handy.

Fr. Robert: Now, what does this all mean? When we talk about meta-characters and strings and things. Quite simply, a regular expression allows you to find, to match, and to replace any string or piece of string, within anything.

Shannon: Really?

Fr. Robert: Yeah. Which is why Perl becomes so popular with web developers. Because a lot of web development is about data manipulation. It’s about looking through stuff and replacing it with something else.

Shannon: Oh, that totally makes sense now. That’s so cool.

Fr. Robert: You also see regular expressions a lot in search engines. The search engine algorithms, the way that you look for it, it’s based on a regular expression. I mean, when you think about it…

Shannon: No way! That makes sense though. They’re looking through that string and trying to find something that matches it.

Fr. Robert: Right. Now if we, before regular expressions, imagine if you were using a search engine. That would only work if you exactly matched it.

Shannon: Oh gosh.

Fr. Robert: Yeah, right?

Shannon: That would be irritating. I would never get anything done.

Fr. Robert: That would be hugely irritating. Exactly. So for example, if I’m looking for something about Google IO, right? I would have to worry about did I put Google IO as all one word? Did I space it? Did I have a slash? Did I put 2014? I don’t have to do that with a regular expression, I can say, look, the string, the connection of meta-characters I’m looking for is Google and IO. Search the entire internet for that particular string and bring me back all the results that match.

Shannon: So that’s why whenever we use Google it’ll say, you know, it’ll give you whatever kind of output its able to find for all those search queries. And then up at the top it’ll be like, “we also found these search queries for google and IO.”

Fr. Robert: Right, it contains the string right. “Did you mean blah blah blah?” and that’s all the regular expression. In fact we’ve got Dex from the chatroom saying hey, regx is great for spam filtering email. Absolutely. We know the phrases that spammers use. Right. So you have spam filters that are searching the entire stack of emails for particular combinations that we know to be used in spam emails. And then we set aside that entire email. So it’s fantastically useful. Even if you don’t exactly know how it works yet. You should understand that in your daily life, you do anything on the internet, you’re using regular expressions.

Shannon: Oh, that’s cool. Yay backend! Understanding.

Fr. Robert: Yay, yay, yay. Now here’s the thing, what we need to be able to do is tell you not only why regular expressions are useful, why they’re important, but we know we’ve got to get people some syntax right? I mean you used it in yours.

Shannon: I did, I used some syntax.

Fr. Robert: But we don’t want people to think it’s this magical line you put that there and that’s why it works and it brings back good things.

Shannon: No, but that always works for me.

Fr. Robert: That always works for you but it won’t necessarily always work for our audience. Which is why, in just a second we’re going to bring in our code warrior Patrick Delehany. What he’s going to do is he’s actually going to sit us down and he’s going to guide us through step by step, what a regular expression contains, what it looks like, and the different operators that we can use within a regular expression. Does that sound like something we want to do?

Shannon: Awesome. Yes that sounds great! I’m so excited.

Fr. Robert: And it’s also, you know, this is what our audience wants. Their feedback…

Shannon: Well if you better understand something, obviously it’s going to make you a lot smarter whenever you have to deal with those kinds of things.

Fr. Robert: Absolutely. Yes. You know what else makes you smarter?

Shannon: What makes- everything?

Fr. Robert: Well, no I, specifically what makes you smarter is doing the same amount of work but with less work.

Shannon: Oh, yeah. I like being able to do that.

Fr. Robert: Smart people, that’s like the definition of smart.

Shannon: Do you have something that will save me some time?

Fr. Robert: Yeah, I think I do. I think I do because you’re a citizen of the internet right?

Shannon: Yes I am.

Fr. Robert: So you like domains, right?

Shannon: Yes.

Fr. Robert: And you know that domains are important right?

Shannon: I Like domains.

Fr. Robert: Is there anything more frustrating than not getting the domain that you want?

Shannon: Ugh! Yeah, that’s happened to me.

Fr. Robert: Or having to create that super lame extra name.

Shannon: Oh totally!

Fr. Robert: Like, let’s say, I want my website to be called snubs! And oh, its snubs235.

Shannon: Snubs is taken.

Fr. Robert: Very good. Actually. Thank you. Snubs is taken. But no, folks. What I’m talking about, I’m talking about a company. A sponsor of Coding 101 that’s going to help you cut your work down when you’re looking for domains.

Shannon: Hey!

Fr. Robert: It’s Hover. Now you want to secure a domain for your great idea. You want something catchy and memorable to represent your online identity. Hover gives you exactly what you need to get that done. You’ll find your perfect domain for your idea so you can get started working on it and move in to the next thing on your to do list. Now people love Hover folks. There they’re a two cows company. This goes way, way back. If you’re a geek you know about this company. When you’re looking for the best place to register a domain name you asks the people who know a lot about domains. Now geeks, developers, designers and programmers all love hover because they know that they’ll have the best tools and support. But you don’t have to be an expert to get a domain name. The surface is simple enough to use that you’ll be comfortable figuring it out by yourself. And the support team is always ready to give you a hand if you need it. Now hover, their thing is that they take all the hassle and friction out of registering a domain. They give you easy to use powerful tools to manage that domain so anyone can do it. And they also get you the perfect domain. Not that junky one, that sort of a second or third choice. But it lets you get the one you want and start building your web presence right away to take control of what people find when they search for you using regular expressions. Or for your business online. Now it’s easy, all you have to do is search for a few keywords and hover will show you the best available options and suggestions. Again this is one of those services that you know, you don’t necessarily have to know exactly what you want, you just have to tell hover what you want to represent and they’ll give you that match. They have a clean and simple website. In less than five minutes you can find the domain name that you want and get it running. And they’ve got a huge variety of domain extensions like .com, .net and country codes to best suit your needs. So you can get, oh, I don’t know, dot just about anything. They also offer valet transfers to make the process painless if you’ve already got your domain registered elsewhere. For free. Hover will take care of the entire process for you and let you know when your domains are settled in your hover account. They’ll transfer all your dns settings or anx settings no matter how many domain names you have. It’s no additional cost. Well folks, the big thing for me is honesty. I want some place that’s honest and that’s hover. They don’t believe in heavy handed up selling or charging you for something that should be part of your service. They include everything you need with your domain. No more and no less. Including a custom email, even for crankyhippo. You’ll get a smart control panel so you can do what you want with your domain with a click of a button. And privacy is included free. On every domain that supports it. So if you ever need a hand you know that hover is going to be there because they have the best customer service around. And now hover offers volume discounts. They’ll give you a discount on your domain renewal. Starting at just 10 domains and going up in value from there. The idea is the more domains you have in your account, the less you should have to pay to renew them. So here’s what we want to do folks. We want you to try hover. We want you to see if it’s as easy for you, for me, for Shannon, for crankyhippo, as it is for the rest of the world. Visit hover today to register your domain name and for 10% off your first purchase, go to hover.com and use our promo code “C101”. We thank hover for their support of Coding 101.

Shannon: We should not allow Brian behind there.

Fr. Robert: We really shouldn’t. This is what happens when you let your TD run free.

Shannon: Crankyhippo.sexy

Bryan: It actually was our code warrior who was in charge of that.

Fr. Robert: Oh, that was, OK, see this is what we- you know what this is as a good time as any to bring our code warrior back in. fresh from his trip to Tahiti.

Patrick Delahanty: Tahiti?

Fr. Robert: Its Mr. Patrick Delahanty folks. He’s one of the engineers- programmers here at twit TV and he’s our Perl guru.

Patrick: Yes, Tahiti is a magical place.

Fr. Robert: Also known as Petaluma.

Patrick: Yeah.

Shannon: They’re so similar to each other.

Fr. Robert: Now Patrick, I know that you went regular expressions- we went over regular expressions. Really quickly in episode 2, let’s slow down just a tiny bit, you know. Go over every nook and cranny. They know the theory, they understand what it is, but now we need to show them exactly how it works in Perl.

Patrick: Yeah, I wouldn’t say we went over it, we just kind of touched on it.

Fr. Robert: We used it.

Patrick: And this episode and the next one are all things with regular expressions.

Fr. Robert: Wait, wait, why would we do that? Why would we dedicate two episodes to regular expressions?

Patrick: Because there is so much to do with it. And yeah. It’s kind of confusing but you can search on it, you can replace, and yeah.

Shannon: Oh wow. Well that’s good. I want to make sure that I get it down before we actually move on to harder stuff.

Fr. Robert: Yeah.

Patrick: Alright, so I’ve got, last week I had the code. And I’ll just run it now. The one where you entered the name of your favorite animal.

Shannon: Donkey.

Fr. Robert: Bunny.

Patrick: Yeah, donkey. Wait, remember this, you said donkey. Okay whatever. Well I’ve modified this. And ill actually run it again, this time I’ll say “My favorite animal is cat”.  You said rabbit.

Shannon: What?

Fr. Robert: What?

Patrick: So I used a regular expression. This will give you another hint of how I did this. My favorite animal: thundercat. You said thunderrabbit. So in this, I’ve used a regular expression to modify any time anybody enters “cat”. To replace it with rabbit. So if I do it again, I say…

Fr. Robert: Oh no, oh boy.

Patrick: There we go. I said “Sarah Lane likes Cats” and it comes out “Sarah Lane likes rabbits”.

Fr. Robert: Okay, now Patrick, I’ve got to ask, it’s a cool example, but would this actually be useful in the real programing world?

Patrick: Oh absolutely. You maybe, normalizing some data, you might want to say okay, they’ve entered this, I want to replace any time this has been entered, with this. Let’s say that they’ve entered something with those smart quotes, you can replace it with normal quotes. You can search for those smart quotes and replace it with normal ones.

Shannon: That’s so handy.

Fr. Robert: Now I get it. It’s useful. Alright, so tell me how this works.

Patrick: Okay, if you look at the code here, this is pretty much the same program we had the last time. But here, I’ve added this search and replace line. This one line. It says- it takes the input which is “animal” and then it has this =~ but then it has the s/ which tells it search and this is going to be a search and replace. So I’m searching for cat, replacing it with rabbit. And the gi, its global and case insensitive, just like before. And down here we have the if statement, this is the same as last week. I haven’t modified that at all. So we’re still looking for if it says bunny or if it says rabbit it says you said animal.

Fr. Robert: Okay, Patrick let me ask you this. I see how this works. So let’s go up a line to animal is = to s/cat/rabbit/gi. Alright, I understand cat/rabbit. Cat is what it’s searching for, rabbit is what it’s going to replace it with. I understand g, so it’s looking through everything. It’s global. And I is the case, correct?

Patrick: Yes.

Fr. Robert: Okay, but whys? Why do you have to specify search in this line, but you don’t have to do it in the next line with the if stantement?

Patrick: Because here it’s telling Perl that it’s a search and replace, whereas down below it’s just checking to see if it’s in here.

Fr. Robert: Oh!

Patrick: So up here, we have to tell it, this s/ because we’re saying, okay, this is going to look for something and immediately replace it with this other thing.

Fr. Robert: Got it.

Shannon: Oh.

Patrick: Down here we’re just looking for that thing. We didn’t do anything.

Fr. Robert: So without the s, it’s just a standard search. To see whether or not that particular string is there. If you add the s, it’s going to search and then it’s going to replace whatever it finds.

Patrick: Yes. And so we’ve got two- I mean this is all still regular expressions. This is just two ways to handle it. And but you can do a lot more complicated things with- you don’t have to put in the actual words. You can do other things. And I’ve got a couple examples of those. If we’re ready to move on to that. Do you have any other questions about this before we take another step?

Shannon: I do. Could I put the g and I before cat and rabbit and would it still work?

Patrick: Uh, no that wouldn’t work.

Shannon: Okay.

Fr. Robert: So what’s the particular syntax you have to use? I mean I get why it’s working the way you put it, but for the folks at home who are doing this for the first time, would we want to move them past the black box stage. Is there a particular pattern that they have to follow when they want to start doing, using regular expressions?

Patrick: Basically what they would do is they would always have the =~, and that tells it that it’s going to be a regular expression thing. And then if it’s an “if” statement, you’re only looking to see if the string is inside, or the string contains that word. So you wouldn’t do a search and replace. So you’d have like, “/padre/” like that.

Fr. Robert: Don’t search and replace Padre.

Patrick: So this would be searching for “padre” within a string. And then at the end, the g is search the entire string. Or just find it once. And then the I is case insensitive. So if I had lowercase padre, uppercase. Doesn’t matter. Now if I put this here, this tells Perl that okay, I need to look for something and replace it with whatever is after that.

Fr. Robert: So I need to tell it what to replace it with. Like snubs of course. Replace padre with snubs.

Patrick: So you couldn’t just put the gi there, because it would say okay this is not valid. There’s nothing…

Shannon: So it’s thinking that you want to replace it with “gi”.

Patrick: Yeah. Even highlighting here, it’s like “okay, well this isn’t done yet.” So I would replace it with  =~ s/Padre/Snubs/gi

Fr. Robert: Naturally.

Patrick: And then, it would be that. And so this would say okay, the whole string in whatever the case, replace padre with snubs.

Fr. Robert: Okay, now I understand why global case insensitive is there, and why it’s useful. Where would you leave it off? Where is there a case where you wouldn’t want it to be global and you wouldn’t want it to be case insensitive?

Patrick: In the search and replace the global comes into play a lot more often, because if I left that out, it would only replace the first occurrence.

Shannon: Never.

Fr. Robert: Okay.

Patrick: So if I- actually let me make that change. I’ll put, I haven’t tried this, let’s hope this works. Favorite animal, cat. And cat, and dog. And cat. You said, so see it only replaced the first one. You said rabbit, and cat, dog and cat. But if I modify this again and put the g, it should globally replace- and cat and cat and cat and dog.

Fr. Robert: Right.

Shannon: Oh, cool.

Patrick: Rabbit and rabbit and rabbit and dog.

Fr. Robert: And just for kicks and giggles, I think we should show them what happens if you take out the caps insensitivity and then add a couple of caps. Just so they understand why that’s in there.

Patrick: Oh sure. So well take that case insensitivity out. Now it’s case sensitive. So I’ll put CAT and cat and CAtttt and Thundercat. So it replaced the second one, because it had, it was all lowercase. It didn’t touch the third and the fourth one, cat was part of the phrase, so it replaced that part.

Fr. Robert: So hopefully the folks at home will see what those qualifiers do. You put that in there, you put the G for the global. When you want to do a search beyond just a single term. And you always have to make it case insensitive unless you only want to replace or search for exact, exact capitalization match.

Shannon: I only want exact kitties.

Fr. Robert: Thank you. Alright, so let’s move on. What else have you got?

Patrick: I have another example here, lets run this. Here we go. This is, what is your favorite movie quote?

Fr. Robert: Get your ass to mars.

Patrick: Get your ass to mars.

Fr. Robert: Sorry, I’m in Arnold mode.

Patrick: and so what I did here was I replaced all the spaces with new lines. So if you look at the code here, this is very simple.

Fr. Robert: Okay.

Shannon: What? That doesn’t look very simple.

Patrick: I’ve got a regular expression here. It’s the same thing. Previously this said cat, and this said bunny. But what I’m doing here is, I’ve got this backslash s, and then backslash n. and as we’ve discussed. We’ve talked about what \n does, and that’s new line.

Fr. Robert: We know what the first s does. We know we’re going to be doing some search and replace. We know what the g does, we know it’s going to be global, but now you’ve got this smiley face in the middle Patrick. What are you trying to pull?

Patrick: this \s, and it’s a backlash, not a slash. But that is a regular expression term to search for a space.

Robert-Shannon: Okay.

Patrick: So this is looking for any space in the string. And the /g tells it all of the spaces in the string. And it’s replacing all of those spaces with \n which is a new line.

Shannon: Oh that’s cool.

Patrick: And so then they just printed out. I’ve got this space here with three dashes. A new line. And then I print the quote.

Fr. Robert: That’s actually, that’s phenomenally, phenomenally important to be able to do. Because you just gave me a way to take a sentence from someone and divide it up into its component parts.

Patrick: Yes.

Fr. Robert: That’s amazing. That’s- and actually I can think of a very simple program that we could give people, which would be to use this function to do a counter. To find out how many words are in a paragraph. That would be something perfect. Because you could use that to separate it out into its pieces, and then count the pieces.

Patrick: Yes. You could easily do like and if statement. I mean, I did a search and replace but you could say, if quote contains a space, then do something. So do something here. And so that means oh you entered a space. And think of how you could use this to say oh wait, you entered an email address with a space, that’s not valid. And so you can, there are all sorts of error checking that can be based off of this too.

Shannon: Oh that’s cool. You could do that on a, if a person enters a phone number incorrectly or social security number.

Patrick: You can use this to strip out spaces from a phone number. Like up here I’ve got the replace it with a \n. if I just leave that blank, it’s stripping all the spaces out.

Fr. Robert: The other thing that you could do Patrick, you don’t have to strip out the spaces. Like for example, let’s say that you wanted to take out periods. For example, someone types out their phone number with 202. Blah blah blah. Blah blah blah. You could use this to just strip out the periods so you get nothing but the numbers.

Patrick: Yes. But you have to be careful about the non-alphanumeric characters in regular expressions. If I just put a period.

Fr. Robert: Right.

Patrick: That is actually a special character for regular expressions. A period matches any character.

Shannon: Oh.

Fr. Robert: We would have to give it the character code so it knows what to look for.

Patrick: Yes, so if I did, let me say I did…

Shannon: So that kind of reminds me of wild cards.

Fr. Robert: It does kind of, right? So you’re doing a search and replace on Shannon.Morse…

Patrick: Shannon any character Morse. And replace it with Snubs.

Fr. Robert: Oh, l like that. Okay, so any time you see Shannon Morse, you’re going to replace it with Snubs.

Patrick: Yeah. So if I ran this, id type “Shannon Morse” it will type “snubs”. If I run it again I type “Shannon1Morse”. Snubs. It matches. And it replaces it with that. So yeah, the period is literally any character. So the way around that would be to put a backslash before it, and now it knows okay, it’s just period. So now when I enter something it’s going to take all the periods out. \. It’s going to look for just the actual period. The dot. And replace it with nothing. So if I run this again. Okay. So I’m going to look for anything with periods. So let’s just enter a phone number, we’ll have 207.786.2411

Fr. Robert: That’s my phone number.

Patrick: That’s time and temperature in Lewiston Maine. So we’ll take that, replace it. It takes out all the dots.

Fr. Robert: Cool.

Shannon: Oh, that’s so handy! I love it.

Fr. Robert: Yeah. Now folks, now you should be able to see the utility of doing this. What we’re talking about is data sanitation. We’re making the data the way we want it so that we can run it through some program that makes it useful.

Shannon: Yes.

Patrick: And because it’s looking specifically for periods, when I put in the dash, it doesn’t do that.

Shannon: Right. That makes sense.

Fr. Robert: But you could write another regular expression that takes away the dashes. That takes away strange characters that people might put in.

Patrick: Yeah, I could. Like parentheses. Speaking of strange characters, we could do, like you saw the matching on space. We could so capital s for non-space. So that means any characters I’ve entered that aren’t a space. Or aren’t a new line.

Fr. Robert: Got it.

Patrick: Anything that is visible. Really.

Shannon: The s is for strange?

Patrick: Capital ones are for non whatever. A lowercase is for that. So capital S is for non-space. S is for space. D is digit. So 0-9. So I could search for any digits and remove them. Capital D is non digit. So abc-. Whatever. And then W is for letters and numbers. Alphanumerics. So this would match a-z and 0-9. But it wouldn’t match +$ and stuff like that. Exclamation mark.

Fr. Robert: I think at this point, now that they understand what regular expressions are and how they use them. I think that what we need to do is recommend that they find an online listing of all the different regular expression modifiers that you can use in order to get what you want. We’ve got a lot of questions in the chat room from people who are asking “well can you use this to get rid of double characters?” yes you can. You just need to figure out exactly how the process is going to work. Can I use it to get rid of parentheses. Of course you can. Again, it’s all going to come down to problem solving. Now that you know what regular expressions do, and what they can do. You have to figure out how to make them solve your problem.

Patrick: Yes, nice. There’s another thing I want to point out here. I had this \s. if we put a + after it, that means it has to find at least one, but if there’s more than one it’ll match that too. So this would take multiple spaces. So if I had, =~ s/Shannon\s+Morse//g; If I had one space in there, or 100 spaces, it would replace them all. It would match that.

Fr. Robert: Fantastic. Now Patrick, I got to ask, we want to cover more regular expressions next week, but what else is left? It would seem as if you’ve given us the basics. You’ve given us enough to get working. What else do we need to know?

Patrick: Well there’s a lot more with regular expressions that we can cover. Including how to actually take something that’s matched and include it in the response. So we could take- I’ll get into it next week. But also I wanted to cover probably the most useful line of Perl that you can ever use. And even if you don’t plan to do Perl programming beyond this module, you’ll still use this line for years to come. It’s a search and replace with Perl. A command line. And it’s the most useful line I’ve ever learned of any programming and I can’t wait to show it.

Shannon: Oh, gosh.

Fr. Robert: Oh, boy. That’s not a tease at all. Patrick we want to thank you so very much for being our code warrior, and thank you for bringing some sanity to regular expression. Because any time we can bring it out of the black box and actually show people how to create these expressions rather than just using the ones that we give them, that’s always a good thing. So thank you so very much. Can you tell the folks at home where they can find you?

Patrick: You can find me, I’m on twitter @PDelahanty and again my website, Animecons.com and fancons.com all written in Perl.

Shannon: Awesome.

Fr. Robert: Now, speaking of things written in Perl, you will be able to find all of the notes from today’s show eventually at our show notes page. We’ll give you the link to wherever we’re going to end up holding those nice juicy tidbits. And where can you find our show notes page?

Shannon: That’s over at twit.tv/code. Yay.

Fr. Robert: Yeah. Yeah. You can also find us on YouTube. You’re going to find us at youtube.com/twitcoding101. There you will be able to find all of our episodes and like for example when we do the special episodes with Jo Heck, you’re definitely going to being able to find that up there. It’s a nice place for you to go and just speed through if you’re ever looking for a reference point.

Shannon: Yes, and I also create a playlist on there so it’s really easy to open up a module playlist and go straight through it if you want to watch episodes 1-8 of a specific programming language. Also, we’re still on ITunes. We’re till one of those top rated shows on ITunes. At least I hope we are. So subscribe if you haven’t already. Definitely check it out over there and of course you can always download it from twit.tv/code as well.

Fr. Robert: Yes you can. Speaking of twit coding. You need to be part of the twit coding G+ community. Go to Plus.google.com/twitcoding101. There you will find over 1000 of our community members. The people who do this for a living. Who want to do it for a living. Or for the people who just want to figure out how coding works. Become part of the community, ask questions, give answers, be part of the awesomeness that is twit coding 101.

Shannon: Yes absolutely. And also, send us your code examples, because I love to show them off on the show and they also help me learn as well. So I really appreciate everyone who sends us their examples. And last but not least we’re also on twitter. What’s your twitter?

Fr. Robert: You can find me at @padresj that’s the little at symbol, padresj.

Shannon: And mine’s easy. Its @snubs.

Fr. Robert: Woo-hoo. And always, we do this show every week, live Thursdays 1:30pm pacific time. PDT. So join us at live.twit.tv. You get to see the pre-show the post show and any bloopers that we may cut out of the regular edit.

Shannon: Like him for forgetting to shut off his mike when he goes to the potty. So gross.

Fr. Robert: Yeah. I did that. And as long as you’re going to be part of the pre-show and post show, why not join us on our chat channel. Irc.twit.tv we pull people straight out of the chatroom.

Shannon: Literally.

Fr. Robert: Literally, like they’re back here and we pull them out. But it’s one of the things that we do. It’s an experiment in the wonderful community on the interwebs.

Shannon: I think that’s about it for today. I’m excited for next week though, I can’t wait.

Fr. Robert: That’s about it. Yeah. I am yeah. I want the super-secret Patrick Delahanty tease me about Perl going to change my life on programming thing. But until that time, I am Father Robert Ballecer.

Shannon: I’m Shannon Morse.

Fr. Robert: End of line.