How to use Lookaheads and Lookbehinds in your Regular Expressions
videos

How to use Lookaheads and Lookbehinds in your Regular Expressions

Tutorial Details
  • Topic: Regular Expressions
  • Format: 18 Minute Screencast
  • Online RegEx Tester: RegExr
Download Source Files

Today, we’ll be reviewing the intricacies of regular expressions. More specifically, we’ll discuss both how and why you should use positive/negative lookahead ands lookbehinds in your regular expressions. Originally meant to be a quick tip, this screencast ended up a bit longer than expected, at around eighteen minutes.


Press the HD button for a clearer picture.
Premium Members: Download this Video ( Must be logged in)

Subscribe to our YouTube page to watch all of the video tutorials!

After viewing the video above, keep in mind that, for any given task, there are a plethora of ways to match your desired text. For example, when matching a Twitter username – as we did in the video – you could also use a non-word-boundary, rather than a positive lookbehind.

\B@\S+\b // matches any Twitter username, like "@nettuts"

The key is to find the right tool for the job.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • John

    Great video! Been playing with regex a lot lately for some screen scrapers. Thanks!

  • http://laroouse.com esranull

    god bless tutsplus thanks a lot

  • http://www.damnseimicolon.com Skye

    good tut. didn’t know about the looking ahead and behind matching. will definitely be using that web tool you’re using. looks much more efficient than me debugging, reload script, debug, reload script…..until my pattern is complete.

  • Christian

    Great video. You never mentioned the co.uk extention (that contains a period)

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Yeah, absolutely. I didn’t want to overload the viewers on creating a valid email expression (as they can get really long, and it was beyond the scope of the tutorial).

      Mostly, I wanted to focus on lookbehinds and lookaheads.

  • http://www.antonagestam.se/ Anton Agestam

    Nice quick tip! :) This is almost the same tool that you’re using but without the legend; http://regex.tastic.org/

  • http://www.tutorialepc.info tutoriale pc

    The other week i’ve learned some lookbehind, but i don’t master them yet.

    @Anton Agestam: thanks for that website. i was testing my regexes in a custom .php file each time.

  • http://www.codemountain.us tashicoder

    Yet another great tutorial from Jeffrey

  • rsludge

    thanks for the screencast
    i knew about this techniques, but used them very seldom. After your video i’ll apply it more

  • Brad

    Excellent tut, thanks Jeffery

  • http://bradbenvenuti.com Brad Benvenuti

    Awesome. Very helpful.

  • http://www.a1media.ca Douglas Helmer

    Jeff,

    This tutorial and the recent “30 CSS Selectors You Must Memorize” tutorial are perfect examples of why I check NetTuts+ every day … because I always get useful productivity enhancing information and insights in subjects I thought I already knew.

    I’ve been using relatively complex regexes for ten years to debug and search through code, but never with lookaheads or lookbehinds because I never bothered to read that far in my O’Reilly “Owl” book. LOL.

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Thanks, Douglas!

  • Proof

    Just a hint, you don’t need to escape the dot in a character class – [\w.]+

  • http://kolombiken.com Ida

    @Jeff
    Didn’t you say that you were going to provide links to JavaScript examples or did I misheard that?
    Nice tutorial anyway. Good job!

  • http://vortrieb.net/ Nils

    Excellent explanation of something I really had a problem to get my head around. Unfortunately I found it a bit late – but now i know where to come back, when I need to refresh my mind.

  • http://www.wsiwebefectivo.com diseño web guadalajara

    You can put look-arounds inside of other look-arounds.This has been known to induce a flight response in certain readers.Positive Lookaround are so-called lookahead or lookbehind assertions.They look for things that go before or after the current match without including them in the match.

  • mailmehe

    Spent long hours to understand by googling, It was hard to get it right.
    Your video made it simple. Thanks a lot.