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

<?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 . "}<br>";
}
?>

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/

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Slashdot
  • StumbleUpon

Comments 13

  1. demiurg wrote:

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

    \1: “\2″

    can i have a cookie? =)

    Posted 18 Oct 2007 at 8:48 pm
  2. Mike Estes wrote:

    Here’s my best shot:

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

    replacement:
    $1: “$2″

    Posted 18 Oct 2007 at 9:02 pm
  3. Jason wrote:

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

    Jason

    Posted 18 Oct 2007 at 9:04 pm
  4. Josh Johnston wrote:

    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);
    ?>

    Posted 18 Oct 2007 at 9:55 pm
  5. Adnan Ali wrote:

    I got it! Should I post the solution here?

    Posted 18 Oct 2007 at 10:33 pm
  6. Adnan Ali wrote:

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

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

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

    Posted 18 Oct 2007 at 10:38 pm
  7. Adnan Ali wrote:

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

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

    /embarrased

    Posted 18 Oct 2007 at 10:54 pm
  8. The-Wildcat wrote:

    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

    Posted 18 Oct 2007 at 11:28 pm
  9. Peter wrote:

    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>”;
    }

    Posted 19 Oct 2007 at 12:14 am
  10. Gareth Heyes wrote:

    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 :)

    Posted 19 Oct 2007 at 12:20 am
  11. Gareth Heyes wrote:

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

    Posted 19 Oct 2007 at 9:36 am
  12. Thijs Lensselink wrote:

    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.

    Posted 19 Oct 2007 at 9:36 am
  13. Gareth Heyes wrote:

    Well done Thijs!

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

    Posted 19 Oct 2007 at 9:44 am

Post a Comment

Your email is never published nor shared. Required fields are marked *

Comment spam protected by SpamBam