PREG_REPLACE

performs regular expression search & replace using PCRE.

Function Installation
CREATE FUNCTION preg_replace RETURNS STRING SONAME 'lib_mysqludf_preg.so';
Synopsis
PREG_REPLACE( pattern , replacement , subject [ , limit ] )
Parameters:
pattern - is a string that is a perl compatible regular expression as documented at: http://us.php.net/manual/en/ref.pcre.php This expression passed to this function should have delimiters and can contain the standard perl modifiers after the ending delimiter.
replacement - is the string to use as the replacement. This string may contain capture group references such as \1. You can also use $1 for these in a similar fashion as in PHP.
subject -is the data to perform the match & replace on
limit - optional number that is the maximum replacements to perform. Use -1 (or leave empty) for no limit.
Returns:
- string - 'subject' with the instances of pattern replaced

- string - the same as passed in if there were no matches

preg_replace is a udf that performs a regular expression search and replace on a given piece of data using a PCRE as the replacement pattern. If limit is not speficied or is -1, preg_replace works on all of the ocurrences of the pattern in the subject data. Otherwise, preg_replace will only replace the first <limit> occurences.

Examples:
SELECT PREG_REPLACE('/(.*?)(fox)/' , '$1dog' , 'the quick brown fox' );

Yields:

+-----------------------------------------------------------------+
| PREG_REPLACE('/(.*?)(fox)/' , '$1dog' , 'the quick brown fox' ) |
+-----------------------------------------------------------------+
| the quick brown dog                                             | 
+-----------------------------------------------------------------+
 * 

SELECT PREG_REPLACE('/\s\s/+', ' ' , products.title FROM products;

Yields: The product names with all of the extra whitespace removed

Note:
Remember to add a backslash to escape patterns that use \ notation. Also, using $ notation makes things a little clearer when using backreferences in the replacement.

Generated on Thu Feb 28 17:35:45 2008 for lib_mysqludf_preg by  doxygen 1.4.6