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

POSIX bracket expressions

From: Using Regular Expressions

Video: POSIX bracket expressions

Back in Chapter 1, when we talked about the history of UNIX, we talked about the POSIX standardization that took place, and part of the POSIX standard was to come up with bracket expressions that would help define sets of characters. They are very similar to the character sets and the shorthand that we've been working with, but they do work a little bit different. First of all, they look very different. They all have square brackets around the outside, then colon next to that, followed by a keyword in the middle. So you can see, for example, that alpha is the keyword for all alphabetic characters. Now, it's a little bit more typing than our shorthand, but you can see how it's useful to get only the letters A through Z, whereas our shorthand, \w, included 0 through 9 and the underscore in that.

POSIX bracket expressions

Back in Chapter 1, when we talked about the history of UNIX, we talked about the POSIX standardization that took place, and part of the POSIX standard was to come up with bracket expressions that would help define sets of characters. They are very similar to the character sets and the shorthand that we've been working with, but they do work a little bit different. First of all, they look very different. They all have square brackets around the outside, then colon next to that, followed by a keyword in the middle. So you can see, for example, that alpha is the keyword for all alphabetic characters. Now, it's a little bit more typing than our shorthand, but you can see how it's useful to get only the letters A through Z, whereas our shorthand, \w, included 0 through 9 and the underscore in that.

We can also specify upper- and lowercase letters. You can see there are keywords for lower and upper. That can be very handy as well. And then there's some for things like pick out all of the printable characters. That can be very useful before we print something; just find the printable characters in this document--characters and the spaces-- or the graphic characters--the things that actually took ink to write. Those are the graphic characters, not the spaces. Or to find all of the control characters, that kind of thing. So it's very commonsense approach. The keywords are easy to read and not hard to memorize, and they do give us a little more specificity than we had by using the other shorthands.

They do work a little bit differently though. When we use these, we don't use them stand-alone; we use them inside a character class. So you would put two sets of square brackets, or we could negate it by putting the negative in front of it. They have to go inside a character class. Very important. The incorrect way is to do it just standing on its own. If you look at that for a second, think about how the regular expression engine sees that when it comes across it.

It comes across it--it says, ah! Open square brackets, this is a character set. Everything that's in here must be part of a character set, and so it treats it like a character set. It doesn't recognize that it's a POSIX bracket expression. So, in general, if you're using POSIX expression, I think it's a really good idea not to mix the POSIX sets with other shorthand sets. Just use one or the other. Pick one and stick with it. If one is not meeting your needs then switch over to the other, but don't mix them, because you can run into problems that way. Now, as far as support goes, you can use them in Perl, PHP, Ruby, and in UNIX, because remember, POSIX was a standardization effort on UNIX, so BREs and EREs should all support the POSIX standard.

However, Java, JavaScript, .NET, and Python do not, at least as far as I know. Now, that's going to create an issue for us, because we're using a JavaScript engine to look at all this, so it's hard for us to look at that. But because it works in UNIX, let me just pop over to UNIX real quick and show you a quick example. So here I am inside Mac OS X, the UNIX shell, and I'm just going to do a real simple in here. I'm just going to do ps aux. Tat's going to list off all the processes that are running on my machine, and I'll pipe that through grep. And in grep, I'm going to tell it that my regular expression that it should use is equal to, and then inside quotes here, I can put my regular expression.

I'm going to tell it I want to find everything that has an S, followed by a character set, and inside that character set is going to be my POSIX expression. Colon, colon, and let's tell it to find everything that is a digit. There we go! It will go through all of my running processes and look for anything that's an S followed by a digit, and there it is. There are the things that it found and that it brought up for us to look at. Now, it does not work of course if we take this away, because now, it's going to find everything that has an S followed by one character, which is either a colon, a D, an I, a G, an I, or a T. It looks that as a literal character set.

So you'll see we get back a whole lot more stuff there. So that illustrates the point of how these POSIX expressions work. If you find that the regular character sets are not able to narrow it down to what you need, these can be a handy way to do it. Now, I think you'll find that a lot of the times either writing your own regular expression or using the other shorthands will do the trick for you, but it is nice to have this tool in your toolbox in case you do need to be a little more specific.

Show transcript

This video is part of

Image for Using Regular Expressions
Using Regular Expressions

59 video lessons · 12449 viewers

Kevin Skoglund
Author

 
Expand all | Collapse all
  1. 2m 18s
    1. Welcome
      56s
    2. Using the exercise files
      1m 22s
  2. 19m 55s
    1. What are regular expressions?
      3m 20s
    2. The history of regular expressions
      6m 40s
    3. Regular expression engines
      2m 44s
    4. Installing an engine
      4m 5s
    5. Notation conventions and modes
      3m 6s
  3. 21m 23s
    1. Literal characters
      6m 39s
    2. Metacharacters
      2m 1s
    3. The wildcard metacharacter
      4m 31s
    4. Escaping metacharacters
      4m 53s
    5. Other special characters
      3m 19s
  4. 31m 26s
    1. Defining a character set
      5m 49s
    2. Character ranges
      4m 49s
    3. Negative character sets
      4m 53s
    4. Metacharacters inside character sets
      5m 12s
    5. Shorthand character sets
      6m 30s
    6. POSIX bracket expressions
      4m 13s
  5. 36m 38s
    1. Repetition metacharacters
      7m 17s
    2. Quantified repetition
      6m 59s
    3. Greedy expressions
      6m 27s
    4. Lazy expressions
      6m 46s
    5. Using repetition efficiently
      9m 9s
  6. 20m 24s
    1. Grouping metacharacters
      4m 14s
    2. Alternation metacharacter
      4m 54s
    3. Writing logical and efficient alternations
      7m 33s
    4. Repeating and nesting alternations
      3m 43s
  7. 19m 19s
    1. Start and end anchors
      7m 21s
    2. Line breaks and Multiline mode
      4m 41s
    3. Word boundaries
      7m 17s
  8. 23m 33s
    1. Backreferences
      8m 57s
    2. Backreferences to optional expressions
      3m 51s
    3. Finding and replacing using backreferences
      7m 16s
    4. Non-capturing group expressions
      3m 29s
  9. 32m 31s
    1. Positive lookahead assertions
      6m 39s
    2. Double-testing with lookahead assertions
      7m 16s
    3. Negative lookahead assertions
      6m 10s
    4. Lookbehind assertions
      6m 26s
    5. The power of positions
      6m 0s
  10. 13m 13s
    1. About Unicode
      4m 19s
    2. Unicode in regular expressions
      4m 41s
    3. Unicode wildcards and properties
      4m 13s
  11. 1h 55m
    1. How to use this chapter
      5m 38s
    2. Matching names
      6m 33s
    3. Matching postal codes
      8m 54s
    4. Matching email addresses
      5m 0s
    5. Matching URLs
      8m 1s
    6. Matching decimal numbers and currency
      6m 45s
    7. Matching IP addresses
      7m 10s
    8. Matching dates
      7m 49s
    9. Matching times
      8m 59s
    10. Matching HTML tags
      8m 34s
    11. Matching passwords
      6m 49s
    12. Matching credit card numbers
      9m 36s
    13. Finding words near other words
      6m 38s
    14. Formatting with Search and Replace, pt. 1
      7m 22s
    15. Formatting with Search and Replace, pt. 2
      4m 15s
    16. Formatting with Search and Replace, pt. 3
      7m 10s
  12. 47s
    1. Goodbye
      47s

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
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Using Regular Expressions.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.