133: Data Types: Regular Expressions.

Take Up Code - Un pódcast de Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Categorías:

A regular expression or regex lets you perform the advanced text operations matching, searching, tokenizing, and replacing. To begin, why would you ever want to use regexes? This is definitely something you’ll want to be careful about. The syntax of a regular expression can easily take several minutes to study even when it only has about 20 characters. Beyond that, and you’ll probably need to create some diagrams to understand what it means. Just one little character can drastically change the meaning. To say that a regex is terse would be a big understatement. But this terseness is also its biggest strength. It can pack a lot of subtle meaning into what may at first look what you would get if a cat walked across your keyboard. Yeah, regexes can be cryptic. Here’s the four basic uses that I’d consider for a regex. If you have a short string or piece of text and need to verify if it matches a known good pattern, then depending on the complexity of the pattern, a regex might be a good idea. Be careful though, I’ve seen regular expressions that try to validate email addresses get completely out of control. If you have a long string, and this could even be a text file, that you want to search for some pattern either to find the pattern or just determine if it exists, then you could find a regex useful. If you have a string that you want to divide into several smaller strings based on one or more patterns then you might want to use a regex. This is called tokenizing. If you want to not just find some matching text but then replace it with something else, then a regex could help. There are no hard rules that I can give you for when to use a regex in your code. I consider regular expressions all the time and I can tell you that I rarely use them. I think where they really shine is when the pattern needs to be configurable. Listen to the full episode for more or you can also read the full transcript below. Transcript There’s a lot of details involved that would make all the stars and parenthesis of the function pointer episodes look downright simple by comparison. So there’s no way I can read exact regular expressions to you. I’ll try to focus on just the concepts and highlight just a few of the special characters so you can get an idea of what you can do with regular expressions. I’ll start referring to a regular expression as a regex. Some people might pronounce this as rejex. I keep the hard G from regular and say regex. To begin, why would you ever want to use regexes? This is definitely something you’ll want to be careful about. The syntax of a regular expression can easily take several minutes to study when it only has about 20 characters. Beyond that, and you’ll probably need to create some diagrams to understand what it means. Just one little character can drastically change the meaning. To say that a regex is terse would be a big understatement. But this terseness is also its biggest strength. It can pack a lot of subtle meaning into what may at first look what you would get if a cat walked across your keyboard. Yeah, regexes can be cryptic. That’s the word I was looking for. Here’s the four basic uses that I’d consider for a regex. ◦ #1 If you have a short string or piece of text and need to verify if it matches a known good pattern, then depending on the complexity of the pattern, a regex might be a good idea. Be careful though, I’ve seen regular expressions that try to validate email addresses get completely out of control. ◦ #2 If you have a long string, and this could even be a text file, that you want to search for some pattern either to find the pattern or just determine if it exists, then you could find a regex useful. ◦ #3 If you have a string that you want to divide into several smaller strings based on one or more patterns then you might want to use a regex. This is called tokenizing. ◦ #4 If you want to not just find some

Visit the podcast's native language site