Regex for Dummies: Day 2

Today, we’ll review the answers to yesterday’s quiz. We’ll then move on to examining PHP and Javascript’s methods for comparing regular expressions against strings.

Day 2: Matching

Be sure to click on the “Full Screen Toggle”.

Quiz

1: What’s the difference between PHP’s preg_match() and JavaScript’s “someVariable.match()”? What is returned from each method?

2: True or False: You should wrap quotations around your regular expressions when working with JavasScript.

3: Homework: What is the difference between “greedy” and “lazy” matching?



19

Comments
  • Allan says:

    Hi,

    how long does it normally take, to get the screencast via iTunes?
    Thx for your work, really appreciate it…

    best regads
    Allan

  • Vasili says:

    For the fifth question, ^(\w+\b.*?){3} also works. :)

    I just wanted to post that, and now I will finish watching the screencast.

  • Steven says:

    Thanks Jeff for another excellent tutorial.

  • qalih says:

    Another good one, keep it up!

  • Meshach says:

    Great job Jeffrey!

  • Mexx says:

    1. someVariable.match() returns the string to be matched whereas preg_match() returns a boolean value.

    2. FALSE

    3. “Generally, regular expressions do greedy matching; they match as many characters as possible. [...] With lazy matching, as few characters as possible are matched while still having the whole regular expression match. The lazy versions of the greedy operators are obtained by adding a question mark.” atnf.csiro.au

    Sorry for cheating ;-)

  • Yeah, these tutorials are very helpful. Thanks Jeffrey :)

  • Matt says:

    Thanks for taking the time to explain Regex for us. :)

    I didn’t know we were supposed to post our answers either, because I put “Lorem ipsum dolor” for 5 out of sheer laziness (laziness is win).

    @Mexx
    I believe he wanted you to look that up, that’s why it’s called “Homework” ;)

  • Symplicity00 says:

    Great Tuts series :D
    And nice quiz for novices like me, its like attending actual class :)
    I’ll definitely stay tuned!!!

  • Dom says:

    Would (\w+\s){2}\w+ be the only way to select any of the first three words, without being lazy and using a question mark?

    Just wanna say thank you very much for these tutorials! :)
    But would it be too much to ask for an index page from which all the days are linked? I know there’s a Screencasts page.

  • Another great one. I’ve been needing this series.

    Homework? I’ll do like in school and copy it from someone during home room :)

  • Jeff Adams says:

    The thing I like about these screencasts is you are literally spoon feeding it.

    i’ve struggled FOR YEARS doing ASP login systems, checking new usernames and stuff but now am thinking PHP will literally save me HOURS of development time!

    Thanks again Jeff, you do an excellent job!

  • M.A.Yoosuf says:

    hi Jeff, thank you for changing the name as per my request, and its an important series for all, including me m thanks alot

  • Willabee says:

    @Jeff Adams – RegExp object has always been available in ASP since VBScript version 5, so you can use what you’ve learnt here without learning PHP… and, of course, regular expressions are also available in ASP.NET whatever language you choose.

    @Jeff, I find the edit theme (Black background) very difficult to read your code with the colour contrast it uses.

  • srigi says:

    Hi Jeff, I got a little recomendation:

    For the screencast lessons use “more complicated” input text, so you can describe more advanced a also more usefull techniques. I recomend for next lesson to setup some HTML code and access for example URLs in tags, or something like that. That will be very usefull for young programers.

  • srigi says:

    In last sentence I meaned “access for example URLs in <a href> tags”.

  • SX says:

    Fun Stuff!

  • Nice screencaset again :) I am glad i am the winner of last quiz yeah i wrote lorem ispum dolum whatever :) “as simple as possible”.

    Answer: php function return 1 as true and 0 as false. true means match occurs and false mean string not matched with regular expression.

    Answer false

    Answer: greedy matching means match as many characters where lazy means as few as possible charters matched.

  • Hugo says:

    Nice screencast. I’ve got one question: is there any difference in formatting the regular expression itself between javascript and php? So if I have a PHP regular expression, is it enough to remove the quotes from it and use it in javascript? (using the appropriate functions such as match() and test() of course)

    Regards!