New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Filtering results with regular expressions

From: MySQL Essential Training

Video: Filtering results with regular expressions

Regular expressions provide a very Now suppose instead of just.

Filtering results with regular expressions

Regular expressions provide a very powerful pattern-matching mechanism, much more powerful than the simple like patterns and also much more complex. Regular expressions are actually a pattern-matching language. It's an incredibly useful feature and it's also a very large subject in itself. In this movie I'll give you a brief overview of the power of regular expressions, but I'm really just scratching the surface. For a more complete discussion of regular expressions. The lynda.com Online Training Library has an excellent course called Using Regular Expressions with Kevin Skoglund.

If you watched the lesson on like expressions, you may remember this statement. I'm going to select the world database here. And. Select name, continent, population, from country where name like underscore a percent order by name. And we'll press go and here we have a list of all of the countries with an a in the second letter of their name in order.

Now suppose instead of just. The letter A in the second position, you wanted all the names with an A, or a B, or a C, or a D in the second position. To do this with like, you would need four like expressions bound together with or operators. And it would end up looking something like this. I'm going to break this into. Multiple lines to make it a little easier. This is actually looking pretty complex isn't it? And, if I run that, we now have.

Oh, there's a C. There just aren't a lot of. B's or D's in the second position are there. But there you have it, it works. Now with a regular expression, this is actually very easy. Instead of all of these or's you would have one where and let me just put this on the same line like that and instead of like. And this pattern here you would say REGEXP which is short for regular expression and a pattern like this.

I'm going to go a through e so we get a few extras here like that. And that's our regular expression. And when I select go, and you see we have some e's in here as well. So you can see this is very powerful. You can do all of this in one line. And it's also pretty cryptic looking. Regular expressions are very powerful. They can be a little complex, but the basics are actually pretty easy to grasp. So in this query, the caret at the beginning. That's an anchor for the beginning of the line.

And so, this anchors the pattern to the beginning of the line so that this dot, which represents one character, it's kind of like the underscore in the like expressions, that represents the very first character in the string. And so the anchor anchors at the beginning and then you have the first character, which can be any character. And then the second character is represented by this bracketed expression that says a-e. And that means any character between a and e. So that's a, b, c, d, and e.

And then we have another dot. Which, as you know, means any character. because we learned that in the first character. Followed by an asterisk, which means, 0 or more of those. And in this case, it's the any character. And then you can actually anchor it at the end. With a dollar sign which anchors the end of the string so I press go, of course this will do the same thing because we didn't really need that anchor because we're matching any character to the end of the string. On the other hand if I just wanted to match anything that ends in a G I can just do this and that matches anything that ends in a G.

Because that G is anchored at the end of the string. And so, when I press Go I just get the names of countries that end in G. So, a period matches any character in that position. For example, if I just do this, that'll match anything. With an O and any character and another O after it. And if I press Go here we get Cocos, Columbia, Comoros, Morocco because of the oro, Solomon, Togo. So, that matches any string with an o followed by one other character followed by another o.

The plus sign matches one or more of a character. So for example, if I change this dot to a C with a plus sign, this'll match anything with one or more C's between two O's. And if I press go here, we get Cocos because it has OCO. We also get Morocco because it has an OCCO. So it matches one or more Cs between the Os. The asterisk matches zero or more of a character. So, if I use an asterisk here instead I'll get any two Os next to each other or any two Os with one or more Cs in between them.

And if I press go here, so we have Cameroon with two O's. We have Cocos because it has O-C-O. And cook, and Morocco. Cook just has the two O's next to each other, and Morocco has two C's in between the O's. So that's the asterisk. It matches zero or more of a particular character. The question mark matches zero or one of a character. So if I change this to a question mark, we won't get Morocco anymore. I'll press go.

We don't get Morocco because it has more than one C. But we do get the ones with no Cs. So we have zero or one. Regular expressions can also specify character classes. A character class is a set of characters that can match a position in a string. So, again, if we want to match the second letter in a string, we can anchor to the beginning and match any character for one position. And then, give it a character class. And then .asterisk in the end of the string.

So inside this character class, I'm just going to select vowels, A, E, I, O, and U. And this will find anything with a vowel in the second position. So we get 165 of those. We have Australia, Bahamas, Belarus. So we have E's, O's, U's, A's. All the vowels in the second position. So this character class allows us to say match any character that's in this list, but just one of them; or if I put a plus sign there it's one or more, and a question mark is zero or one, or an asterisk is zero or more.

You can also specify of course a range. Of letters and we did this before when we said a through e, the dash, so that's a range of letters, so a, b, c, d and e. So, if I press go we just get those. There are also a number of predefined classes, for example if I want to match. A space character. These have to be in an extra set of brackets and between colons. And I put in the word space and that actually matches any white space.

A space character, a tab character, a new line character, and a few others. So, if I take out all of this other stuff because we're not probably going to find a space as the second position. But this'll give us any strings with a space in them anywhere. And so this has just the ones with more than one word. Other predefined classes include these classes here. You'll find this list and other information in the MySQL documentation at this URL.

R like is a synonym for Reg X. And it looks like this. And it works exactly the same. You'll see this occasionally. It's included mostly for legacy purposes. The R like keyword was originally included for compatibility with an older database system called MSQL. For now its best to use the Regexp keyword

Show transcript

This video is part of

Image for MySQL Essential Training
MySQL Essential Training

60 video lessons · 4670 viewers

Bill Weinman
Author

 
Expand all | Collapse all
  1. 4m 22s
    1. Welcome
      1m 3s
    2. Using the exercise files
      1m 31s
    3. What is MySQL?
      1m 48s
  2. 45m 37s
    1. Installation overview
      3m 16s
    2. Installing XAMPP on Windows
      5m 55s
    3. Installing XAMPP on the Mac
      6m 38s
    4. Setting up MySQL users
      11m 31s
    5. Installing SID on Windows
      5m 43s
    6. Installing SID on the Mac
      6m 6s
    7. Installing time zone support in MySQL on Windows
      6m 28s
  3. 45m 43s
    1. The SELECT statement
      3m 57s
    2. Selecting rows
      4m 57s
    3. Selecting columns
      3m 8s
    4. Sorting results with ORDER BY
      2m 58s
    5. Filtering results with WHERE
      3m 52s
    6. Filtering results with LIKE and IN
      3m 41s
    7. Filtering results with regular expressions
      8m 21s
    8. Inserting rows
      4m 9s
    9. Updating rows
      2m 21s
    10. Deleting rows
      2m 25s
    11. Literal strings
      3m 12s
    12. Understanding NULL
      2m 42s
  4. 41m 47s
    1. Creating a database
      4m 30s
    2. Creating a table
      7m 18s
    3. Creating indexes
      6m 8s
    4. Controlling column behavior with constraints
      4m 46s
    5. Creating an ID column
      6m 58s
    6. Using foreign key constraints
      7m 58s
    7. Altering a table
      4m 9s
  5. 28m 56s
    1. What are data types?
      4m 1s
    2. Numeric types
      5m 21s
    3. String types
      2m 58s
    4. Date and time types
      7m 2s
    5. Bit type
      2m 26s
    6. Boolean values
      2m 15s
    7. Enumeration types
      4m 53s
  6. 32m 34s
    1. String functions
      6m 57s
    2. Numeric functions
      6m 2s
    3. Date and time functions
      4m 12s
    4. Time zones in MySQL
      3m 37s
    5. Formatting dates
      1m 51s
    6. Aggregate functions
      5m 45s
    7. Flow control with CASE
      4m 10s
  7. 7m 6s
    1. Maintaining database integrity with transactions
      4m 46s
    2. Using transactions for performance
      2m 20s
  8. 16m 49s
    1. Updating a table with a trigger
      5m 11s
    2. Preventing automatic updates with a trigger
      7m 29s
    3. Logging transactions with a trigger
      4m 9s
  9. 14m 11s
    1. Creating a simple subselect
      3m 23s
    2. Searching within a result set
      3m 53s
    3. Creating a view
      3m 32s
    4. Creating a joined view
      3m 23s
  10. 12m 26s
    1. Understanding MySQL stored routines
      2m 0s
    2. Creating a stored function
      4m 34s
    3. Creating a stored procedure
      5m 52s
  11. 14m 4s
    1. The multi-platform PDO interface
      3m 44s
    2. Executing the SQL
      4m 8s
    3. Implementing auto-increment IDs
      2m 3s
    4. Using a stored funciton
      4m 9s
  12. 1m 3s
    1. Goodbye
      1m 3s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold

Are you sure you want to delete this note?

No

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.