The Spanner logo
    • Home
    • Blog
      • Blog home
      • RSS
    • Login
    • Home
    • Blog
      • Blog home
      • RSS
    • Login
    The Spanner logo

    The Spanner
    Web security blog

    Made by Gareth Heyes
    Follow me on Twitter: @garethheyes

    Javascript for hackers!

    Hackvertor logo
    Shazzer logo
    My Github account
    Recent posts
    Pure-CSS 3D world collision detection How to write a Hackvertor tagIntroducing Feedworm: A Privacy-First RSS Reader That Lives in DevToolsSpeedy RSVP extensionAutoVaderHackvertor history and tag finderShadow Repeater v1.2.3 releaseBurp Hackvertor v2.1.24 releaseHacking roomsXSSing TypeErrors in SafarivalueOf: Another way to get thisMaking the Unexploitable Exploitable with X-Mixed-Replace on FirefoxThe curious case of the evt parameterCSS-Only Tic Tac Toe ChallengeRewriting relative urls with the base tag in SafariBypassing DOMPurify with mXSSNew IE mutation vectorHow I smashed MentalJSMentalJS DOM bypassAnother XSS auditor bypassXSS Auditor bypassBypassing the IE XSS filterUnbreakable filterMentalJS bypassesmXSSJava SerializationBypassing the XSS filter using function reassignmentRPOSandboxed jQueryX-Domain scroll detection on IE using focusEpic fail IEnew operatorDecoding complex non-alphanumeric JavaScriptHacking FirefoxDOM ClobberingBypassing XSS AuditorThe evolution of codeNon-Alpha PHP in 6-7 charsetTweetable PHP-Non AlphaMentalJS for PHP

    Regular expression challenge

    By Gareth Heyes (@hackvertor)

    Published 18 years 10 months ago • Last updated March 22, 2025 • ⏱️ 2 min read

    ← Back to articles

    After the success of my "a bit of fun" challenge, a few people asked for some more challenges. So I was answering a question on a mailing list that I'm a member of and I thought it would be a good topic for a little challenge and help sharpen everyone's regular expression skills.

    The rules

    Only one valid regular expression is allowed and no other code can be used other than the regular expression itself.

    The input

    <pre lang="php"> <?php $strings = array('from sutton-in-craven to anywhere', 'from leeds, west yorkshire to harrogate, north yorkshire', 'to leeds via guisley from harrogate','from harrogate to leeds', 'via guisley from harrogate to leeds','from harrogate to leeds'); foreach($strings as $string) { $output = preg_replace("/YOUR REG EXP HERE/i", 'YOUR REPLACEMENT STRING HERE', $string); echo "{" . $output . "} "; } ?> </pre>

    The output

    This is what the output should look like:- { from: "sutton-in-craven" to: "anywhere" } { from: "leeds, west yorkshire" to: "harrogate, north yorkshire" } { to: "leeds" via: "guisley" from: "harrogate" } { from: "harrogate" to: "leeds" } { via: "guisley" from: "harrogate" to: "leeds" } { from: "harrogate" to: "leeds" }

    Enjoy and good luck :)

    The winner

    Well I thought how can I judge the winner? It can't really be the first one because of timezones so I thought the neatest and smallest regular expression. At the moment there is no prize but if there are any companies out there willing to purchase a book for the winner and for future contests let me know and I shall place your logo and link on this and future contests.

    At first I thought the winner was Jason but I double checked his regular expression and it didn't seem to give the correct output. So the winner is.... Adnan Ali with /(to|from|via) (.*?)(?= (?1) |$)/i

    Well done Adnan!!! Adnan has a blog which is available here:- http://www.adnanali.net/

    ← Back to articles