lib_mysqludf_preg is a useful performance optimization for those applications that are already performing these regular expression matches in a high level language (ie. PHP) on the client side. It is also helpful when there is a need to capture a parenthesized subexpression from a regular expression, or simply as a slight performance boost over the builtin RLIKE/REGEXP functions.
If mysql is an unusual place, you might need to add --with-mysql=<mysql directory>/bin/mysql_config.
Similarly, if licpcre is in an unusual place, --with-pcre can be added.
Example (for osx using fink): ./configure --with-pcre=/sw --with-mysql=/sw/bin/mysql_config
For more information, please consult the doc/INSTALL.windows file in the source package.
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. | |
subject | -is the data to perform the match & capture on | |
group | - is the capture group that should be returned. This can be a numeric capture group or a named capture group. Numeric groups should be passed in as integers while named groups should be strings. If not speficied, this defaults to 0, which will capture the entire matching regular expression. | |
occurence | - which match of the regex to perform capture on. This is useful for subjects that have multiple matches of the pattern. If not speficied, this defaults to 1, which will capture the requested group, from the first matching occurence of the pattern. |
- string that is the entire portion of subject which matches the pattern - if 0 is passed in as the group and pattern matches subject
- NULL - if pattern does not match the subject or group is not a valid capture group for the given pattern and subject.
Yields:
+----------------------------------------------------------+ | PREG_CAPTURE('/(.*?)(fox)/' , 'the quick brown fox' ,2 ) | +----------------------------------------------------------+ | fox | +----------------------------------------------------------+
SELECT PREG_CAPTURE( '/"([^"]+)"/' , 'the "quick" brown fox "jumped" over the "lazy" dog' , 1,2 );
+--------------------------------------------------------------------------------------------+ | PREG_CAPTURE( '/"([^"]+)"/' , 'the "quick" brown fox "jumped" over the "lazy" dog' , 1,2 ) | +--------------------------------------------------------------------------------------------+ | jumped | +--------------------------------------------------------------------------------------------+
SELECT PREG_CAPTURE( '/b[^\s]+/' , 'the quick brown fox jumped over' )
Yields:
+------------------------------------------------------------------+ | PREG_CAPTURE( '/b[^\\s]+/' , 'the quick brown fox jumped over' ) | +------------------------------------------------------------------+ | brown | +------------------------------------------------------------------+
pattern | - is a string that might be a perl compatible regular expression as documented at: http://us.php.net/manual/en/ref.pcre.php |
0 - the pcre is NULL, empty, or a bad regex
Yields:
+---------------------------------------------------------------+ | PREG_CHECK('/The quick brown fox/i' ) | +---------------------------------------------------------------+ | 1 | +---------------------------------------------------------------+
SELECT * from patterns WHERE PREG_CHECK( pattern );
Yields: all of the rows containing valid pcre's.
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. | |
subject | -is the data to perform the match & position capture on | |
group | - the capture group whose position that should be returned. This can be a numeric capture group or a named capture group. Numeric groups should be passed in as integers, while named groups should be strings. 0 should be used to request to position of the entire matching expression. This parameter defaults to 0. | |
occurence | - which match of the regex to perform capture on. This is useful for subjects that have multiple matches of the pattern. This parameter defaults to 1. |
- NULL if pattern does not match the subject or group is not a valid capture group or the occurence is larger than the number of matches for the given pattern and subject.
Yields:
+-----------------------------------------------------------+ | PREG_POSITION('/(.*?)(fox)/' , 'the quick brown fox' ,2 ) | +-----------------------------------------------------------+ | 17 | +-----------------------------------------------------------+
SELECT PREG_POSITION('/"[^"]+"/' , '"quick","brown","fox" "jumped"',0,4)
Yields:
+-------------------------------------------------------------------+ | PREG_POSITION('/"[^"]+"/' , '"quick","brown","fox" "jumped"',0,4) | +-------------------------------------------------------------------+ | 23 | +-------------------------------------------------------------------+
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. |
- string - the same as passed in if there were no matches
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
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. | |
subject | - is the data to perform the test on. |
0 - no match
Yields:
+---------------------------------------------------------------+ | PREG_RLIKE('/The quick brown fox/i' , 'the quick brown fox' ) | +---------------------------------------------------------------+ | 1 | +---------------------------------------------------------------+
SELECT * from products WHERE PREG_RLIKE( '/organic/i' , products.title )
Yields: all of the products with 'organic' in their titles
+--------------------------+ | LIB_MYSQLUDF_PREG_INFO() | +--------------------------+ | lib_mysqludf_preg 0.6.1 | +--------------------------+