You are here: Re: who is good at regular expressions? « PHP Programming Language « IT news, forums, messages
Re: who is good at regular expressions?

Posted by Chung Leong on 09/07/05 17:37

That is actually a very tricky regexp to write, because the presence of
the colon at the end of a word changes its meaning, from being part of
the previous values to that of a keyword. With the example given, it'd
be hard to keep the regexp engine from appending "institution" to "Jane
Smith."

The trick here is to reverse the string, then reverse the values after
they've been extracted. Since keywords don't contain white-spaces, it's
easy to tell the regexp where it should stop.

$s = "author:Jane Smith institution:university of Cambridge year:1976";
$r = strrev($s);
preg_match_all("/\s*(.*?)\s*:\s*(\S+)/", $r, $m, PREG_SET_ORDER);
foreach($m as $set) {
$key = strrev($set[2]);
$value = strrev($set[1]);
$values[$key] = $value;
}
print_r($values);

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация