27 January 2013

Regular expressions

MySQL

Change telephone numbers of format 123 4567 to 020 123 4567

SELECT qname, tel FROM `restaurants` WHERE tel REGEXP  '^[0-9]{3}\ [0-9]{4}$'

UPDATE `restaurants` SET `tel` = CONCAT("020", telWHERE tel REGEXP  '^[0-9]{3}\ [0-9]{4}$'

Javascript

//$content = preg_replace('/(\[caption.*\])/', '', $content);
// () Grouping
// [ ] Character class
// ^ in [ ] is a NOT
// . Match any character (except newline)
// * Match preceding character 0 or more times
// + Match preceding character 1 or more times
// ? Match 1 or 0 times
// | OR(?)
// $ match end of input
// /i 
(.+?) = match any charchter more than once
// \s Match any white space character
// preg_replace('/(.+?)+(<\/tag>)/i', '', $string);
//$imgs = preg_match('/]+\>/i', $content);
//$howMany = preg_match_all('/]+>/i', $content, $matches, PREG_PATTERN_ORDER);

//$howMany = preg_match_all('/alt=\"([^\"]+)\".*src=\"([^\"]+)\".*width=\"([^\"]+)\".*height=\"([^\"]+)\"/i', $content, $images, PREG_PATTERN_ORDER);

No comments: