Regular expression challenge

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

";
}
?>

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/

13 Responses to “Regular expression challenge”

  1. demiurg writes:

    /(?:(from|via|to)) ((?:[^ ]| (?!(?:to|from|via) ))*)/i

    \1: “\2”

    can i have a cookie? =)

  2. Mike Estes writes:

    Here’s my best shot:

    regex:
    /(from|to|via) (.+?(?=( ?from | ?to | ?via |$)))/i

    replacement:
    $1: “$2”

  3. Jason writes:

    “/from (.*) [via (.*)]? to (.*)/”
    Output regex:
    “via \”\\2\” from \\1 to \\3″

    Jason

  4. Josh Johnston writes:

    I had to add the U modifier at the end to make this one work so there is probably something better.
    Enjoy

    <?php
    $output = preg_replace(‘/(from|via|to)\s(.+)(?=(?: from| via| to|$))/iU’, ‘$1: “$2″‘, $string);
    ?>

  5. Adnan Ali writes:

    I got it! Should I post the solution here?

  6. Adnan Ali writes:

    Ah, clever, comments are moderated so you see them first.

    Here it is:
    match:
    “/((to|from|via) )(.*?)( ?)(?=(?1)|$)/i”

    replace:
    ‘\2: “\3″\4’

  7. Adnan Ali writes:

    Sorry for the spam, but I figured that could be simplified to:

    “/(to|from|via) (.*?)(?= (?1) |$)/i”
    ‘\1: “\2″′

    /embarrased

  8. The-Wildcat writes:

    foreach($strings as $string) {
    $output = preg_replace(“/(from|to|via)\s{1}([a-z\-]{0,})(?(?=\,)([a-z\s,]{0,16}\S|to))/i”,
    ‘ $1: “$2$3” ‘, $string);
    echo “{” . $output . “}<br>”;
    }

    Very bad, but it works xD
    And now, i go to bed

  9. Peter writes:

    foreach($strings as $string) {
    $output = preg_replace(“/(from|to|via) (\S*+(?: (?!from\b|to\b|via\b)\S*+)*+)/i”, ‘\1: “\2″‘, $string);
    echo “{ ” . $output . ” }<br>”;
    }

  10. Gareth Heyes writes:

    Well done everyone who has entered so far, I shall post my solution tomorrow and let you know which one I thought was best. Some of them look at lot better than mine 🙂

    I can’t offer any prizes unless I could get a sponsor 🙁 only my praise 🙂

  11. Gareth Heyes writes:

    Here’s mine:-
    $output = preg_replace(“/\s?\b(from|to|via)\b \b([\w-]+([,][\s][\w]+([\s][\w]+)?)?)/i”, ‘ $1: “$2” ‘, $string);

  12. Thijs Lensselink writes:

    Bit late as usual. But here goes:

    reg exp: (from|to|via)\s(.+?(?=( ?from | ?to | ?via |$)))
    Replacement: ${1}: “${2}”

    By the way i got an error on submitting
    Warning: join() [function.join]: Bad arguments. in /path/to/plugins/spambam/spambam.php on line 43
    We don’t allow comment spam here. Javascript is required to submit a comment.

  13. Gareth Heyes writes:

    Well done Thijs!

    Thanks for point that error out, I’ll look into that bug.