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/




Comments 13
/(?:(from|via|to)) ((?:[^ ]| (?!(?:to|from|via) ))*)/i
\1: “\2″
can i have a cookie? =)
Posted 18 Oct 2007 at 8:48 pm ¶Here’s my best shot:
regex:
/(from|to|via) (.+?(?=( ?from | ?to | ?via |$)))/i
replacement:
Posted 18 Oct 2007 at 9:02 pm ¶$1: “$2″
“/from (.*) [via (.*)]? to (.*)/”
Output regex:
“via \”\\2\” from \\1 to \\3″
Jason
Posted 18 Oct 2007 at 9:04 pm ¶I had to add the U modifier at the end to make this one work so there is probably something better.
Enjoy
<?php
Posted 18 Oct 2007 at 9:55 pm ¶$output = preg_replace(’/(from|via|to)\s(.+)(?=(?: from| via| to|$))/iU’, ‘$1: “$2″‘, $string);
?>
I got it! Should I post the solution here?
Posted 18 Oct 2007 at 10:33 pm ¶Ah, clever, comments are moderated so you see them first.
Here it is:
match:
“/((to|from|via) )(.*?)( ?)(?=(?1)|$)/i”
replace:
Posted 18 Oct 2007 at 10:38 pm ¶‘\2: “\3″\4′
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 ¶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
Posted 18 Oct 2007 at 11:28 pm ¶And now, i go to bed
foreach($strings as $string) {
Posted 19 Oct 2007 at 12:14 am ¶$output = preg_replace(”/(from|to|via) (\S*+(?: (?!from\b|to\b|via\b)\S*+)*+)/i”, ‘\1: “\2″‘, $string);
echo “{ ” . $output . ” }<br>”;
}
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 ¶Here’s mine:-
Posted 19 Oct 2007 at 9:36 am ¶$output = preg_replace(”/\s?\b(from|to|via)\b \b([\w-]+([,][\s][\w]+([\s][\w]+)?)?)/i”, ‘ $1: “$2″ ‘, $string);
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
Posted 19 Oct 2007 at 9:36 am ¶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.
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