PREG_CAPTURE

capture a parenthesized subexpression from a PCRE pattern

Function Installation
CREATE FUNCTION preg_capture RETURNS STRING SONAME 'lib_mysqludf_preg.so';
Synopsis
PREG_CAPTURE( pattern , subject, group )
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.
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 INT while named groups should be strings.
Returns:
- string that was captured - if there was a match and the desired capture group is valid

- 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.

preg_capture is a udf that captures parenthesized sub-expressions from a pcre pattern.

Examples:
SELECT PREG_CAPTURE('/(.*?)(fox)/' , 'the quick brown fox' ,2 );

Yields:

+----------------------------------------------------------+
| PREG_CAPTURE('/(.*?)(fox)/' , 'the quick brown fox' ,2 ) |
+----------------------------------------------------------+
| fox                                                      | 
+----------------------------------------------------------+
 * 

SELECT PREG_CAPTURE('/(?:^|\s)(organic)(.*?[^\s]+)\s/i' ,products.title, 2 ) AS w FROM products HAVING w IS NOT NULL;

Yields: the word following organic in all of the product names

SELECT PREG_CAPTURE('/(?:^|\s)(organic)(?P<follow>.*?[^\s]+)\s/i' ,products.title, 'follow' ) AS w FROM products HAVING w IS NOT NULL;

Yields: the same as above but with using named capture group

Note:
Remember to add a backslash to escape patterns that use \ notation

Generated on Fri Sep 7 16:51:44 2007 for lib_mysqludf_preg by  doxygen 1.4.6