Regular expression challenge
Thursday, 18 October 2007
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/
No. 1 — October 18th, 2007 at 8:48 pm
/(?:(from|via|to)) ((?:[^ ]| (?!(?:to|from|via) ))*)/i
\1: “\2”
can i have a cookie? =)
No. 2 — October 18th, 2007 at 9:02 pm
Here’s my best shot:
regex:
/(from|to|via) (.+?(?=( ?from | ?to | ?via |$)))/i
replacement:
$1: “$2”
No. 3 — October 18th, 2007 at 9:04 pm
“/from (.*) [via (.*)]? to (.*)/”
Output regex:
“via \”\\2\” from \\1 to \\3″
Jason
No. 4 — October 18th, 2007 at 9:55 pm
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);
?>
No. 5 — October 18th, 2007 at 10:33 pm
I got it! Should I post the solution here?
No. 6 — October 18th, 2007 at 10:38 pm
Ah, clever, comments are moderated so you see them first.
Here it is:
match:
“/((to|from|via) )(.*?)( ?)(?=(?1)|$)/i”
replace:
‘\2: “\3″\4’
No. 7 — October 18th, 2007 at 10:54 pm
Sorry for the spam, but I figured that could be simplified to:
“/(to|from|via) (.*?)(?= (?1) |$)/i”
‘\1: “\2″′
/embarrased
No. 8 — October 18th, 2007 at 11:28 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
And now, i go to bed
No. 9 — October 19th, 2007 at 12:14 am
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>”;
}
No. 10 — October 19th, 2007 at 12:20 am
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 🙂
No. 11 — October 19th, 2007 at 9:36 am
Here’s mine:-
$output = preg_replace(“/\s?\b(from|to|via)\b \b([\w-]+([,][\s][\w]+([\s][\w]+)?)?)/i”, ‘ $1: “$2” ‘, $string);
No. 12 — October 19th, 2007 at 9:36 am
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.
No. 13 — October 19th, 2007 at 9:44 am
Well done Thijs!
Thanks for point that error out, I’ll look into that bug.