Tip

DB2 regular expressions for the SQL-challenged

This tip was originally posted on Search390.com.

Quiz: Regular expressions refer to...

  1. Happy faces at end of Metamucil commercials.
  2. Charles Dickens' prequel that began "It was the most ordinary of times."
  3. Unixy pattern matching that buries DB2's LIKE wildcards. <=== PICK ME!!!!!

Fans of MySQL and mSQL taunt DB2 users about DB2's lack of support for the non-standard RLIKE/REGEXP facility in SELECT and other SQL statements.

Peruse http://www.mysql.com/doc/en/Pattern_matching.html.

Here's a comparison of standard SQL LIKE vs MySQL's RLIKE/REGEXP:

SQL LIKE pattern matching...

 
_           Matches any single character 
%           Matches 0 or more characters of any value 
\          Escapes special characters  

All other characters match themselves

MySQL RLIKE/REGEXP pattern matching...

^           Match the beginning of the string  
$           Match the end of string  
. (period)  Match any single character  
[...]       Match any character between the brackets  
[^...]      Match any character not between the brackets  
E*          Match zero or more instance of pattern E  
E+          Match one or more instance of pattern E  
E?          Match zero or one instance of pattern E  
E1 | E2     Match E1 or E2  
E{m}        Match m instances of E  
E{,n}       Match zero to n instances of E  
E{m,}       Match m or more instances of E  
E{m,n}      Match m to n instances of E  
(...)       Group elements in to one element 

All other characters match themselves.

Expression results:

 
"abc"       REGEXP   "a.c"        returns 1
"abc"       REGEXP   "[a-z]"      returns 1
"abc"       REGEXP   "[^a-z]"     returns 0
"abc"       REGEXP   "^abc$"      returns 1
"abcd"      REGEXP   "^abc$"      returns 0
"abc"       REGEXP   "(abc){2}"   returns 0
"abcabc"    REGEXP   "(abc){2}"   returns 1

For dessert partake of San Diego State University's Roger Whitney's compendium.

Fans of Informix may have rolled their own UDRs (User Defined Routines) to extend SQL functionality. DB2 users can do likewise with UDFs (User Defined Functions).

Read more at Grok.

DB2 users *may* find various DB2 Extender features barely sufficient, especially character and word matching.

Settle for http://publib.boulder.ibm.com/cgi-bin/bookmgr/download/DESU1A00.pdf.

DB2 users can thank IBMer Knut Stolze for his very recent contribution on the use of regular expressions in SQL queries via UDFs. Complete with SQL and C source code examples and the following UDFs:

mi_boolean      regexp_match_char (mi_lvarchar *str_txt, mi_lvarchar *exp_txt);
mi_boolean      regexp_match_clob (MI_LO_HANDLE *src, mi_lvarchar  *re);
mi_lvarchar  * regexp_replace_char_n (mi_lvarchar *str_txt,
                       mi_lvarchar *exp_txt, mi_lvarchar *rep_txt, mi_integer limit);
mi_lvarchar  * regexp_replace_char (mi_lvarchar *str_txt, mi_lvarchar *exp_txt,
                       mi_lvarchar *rep_txt);
MI_LO_HANDLE * regexp_replace_clob_n (MI_LO_HANDLE *str_txt,
                       mi_lvarchar *exp_txt, mi_lvarchar *rep_txt, mi_integer limit);
MI_LO_HANDLE * regexp_replace_clob (MI_LO_HANDLE *str_txt,
                       mi_lvarchar *exp_txt, mi_lvarchar *rep_txt);
mi_lvarchar  * regexp_extract_n (mi_lvarchar *str_txt, mi_lvarchar *exp_txt, 
                       mi_integer limit, MI_FPARAM *fParam);
mi_lvarchar  * regexp_extract (mi_lvarchar *str_txt, mi_lvarchar *exp_txt,
                       MI_FPARAM *fParam); 
mi_lvarchar  * regexp_split_n (mi_lvarchar *str_txt, mi_lvarchar *exp_txt,
                       mi_integer  limit, MI_FPARAM *fParam);
mi_lvarchar  * regexp_split (mi_lvarchar *str_txt, mi_lvarchar *exp_txt,
                       MI_FPARAM *fParam);

Gorge on http://www7b.boulder.ibm.com/dmdd/library/techarticle/0301stolze/0301stolze.html.

Investigate open source for z/OS & USS, especially grep & vim which both invoke regexp.

Cram tonight on http://www-1.ibm.com/servers/eserver/zseries/zos/unix/redbook/sg245944.pdf.

The modified components of open source for z/OS are available if you politely entreat IBMer Steve Stiert via email to stiert@us.ibm.com.

Spelunk z/OS public forums referenced at http://www-1.ibm.com/servers/eserver/zseries/zos/unix/redbook.

For extra credit envision stored procedures invoking regexp functionality as an alternative to UDFs.

About the Author

Jim Keohane is president of New York consulting company Multi-Platforms, Inc. His company specializes in commercial software development/consulting with emphasis on cross-platform and performance issues.

For More Information

  • Feedback: E-mail the editor with your thoughts about this tip.
  • More tips: Hundreds of free DB2 tips and scripts.
  • Tip contest: Have a DB2 tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize -- submit your tip today!
  • Ask the Experts: Our SQL, database design, Oracle, SQL Server, DB2, metadata, and data warehousing gurus are waiting to answer your toughest questions.
  • Forums: Ask your technical DB2 questions--or help out your peers by answering them--in our active forums.
  • Best Web Links: DB2 tips, tutorials, and scripts from around the Web.

This was first published in April 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.