• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Regular Expressions, Part 2

    August 15, 2022 Mike Larsen

    In the first part of this series, I showed how to replace characters in a string using SQL and regular expressions. This time, I’ll show other regular expressions that are available for us to use. The regular expression functions I’ll show are REGEXP_COUNT and REGEXP_LIKE and the examples come from production programs that I’ve recently implemented.

    In the first example (Figure 1), I want to get a count of the number of occurrences of a pattern within a string. To make this more interesting, I’m searching for two different patterns within a sting. To accomplish this objective, I used REGEXP_COUNT to get a count of how many times the pattern appears in a string.

    Figure 1. Get a count of the number of occurrences of a pattern within a string

    Before showing the result of this statement, let’s analyze the arguments being used. The first argument is the string to be searched. In a production program, this would likely be a variable or a field from a file or table. The second argument indicates what I’m looking for in the string. In this case, I’m looking for either [CR] or [GS]. The pipe symbol is used to indicate this is an ‘or’ statement. The third argument directs the function to start looking for these patterns in the first position of the string. Finally, the last argument I specified indicates that I want to ignore the case of the letters being searched. The result of the statement is shown in Figure 2.

    Figure 2. Result of the REGEXP_COUNT function

    In the next example, I’ll show the REGEXP_LIKE function (Figure 3). With this function, I’m checking for the existence of a pattern with a string. If the pattern is found, the function returns a ‘Y’. If the pattern isn’t found, the function returns a blank. Let’s look at this function using the same string I used in the first example.

    Figure 3. REGEXP_LIKE function

    The arguments provided are the same as I used in the REGEXP_COUNT, so they don’t need further explanation. When I run this statement (Figure 4), a ‘Y’ is returned as the pattern is found in the string.

    Figure 4. Result of the REGEXP_LIKE function

    So, what if the pattern isn’t found? I’ll show that scenario in Figure 5.

    Figure 5. REGEXP_LIKE function

    When I run this statement, a blank is returned (Figure 6).

    Figure 6. Result of the REGEXP_LIKE function

    The final example is from a query I wrote to identify customers who have the letters ‘DBA’ at the beginning of an address field. On the surface, this seems like a simple request, but there is a twist. During the data entry process, this field may have been populated as ‘DBA’, ‘D/B/A’, (DBA)’, ‘DBA:’, or other ways. To identify these customers, I wrote the statement shown in Figure 7.

    Figure 7. Identify customers with ‘DBA’ in the address field

    For privacy purposes, I anonymized the library, table name, and field names in the query. I also anonymized company names in the results (Figure 8).

    Figure 8. Results of the customer query

    Hopefully the examples provided in this series are helpful in getting you started with regular expressions. I’ve found them to be very useful in my programming and will continue to use them where it makes sense. I’m always happy to have more tools accessible to me.

    RELATED STORY

    Guru: Regular Expressions, Part 1

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, FHG, Four Hundred Guru, IBM i, SQL

    Sponsored by
    Computer Keyes

    Fax Directly from your IBM i

    KeyesFax is a full function automated IBM i fax system. Spooled files are burst by fax number and auto transmitted with overlays.  It combines both a send and receive facsimile processing system with a complete image package.

    The fax software will edit, send, receive, display, print, and track fax documents or images using any standard IBM i without additional expensive hardware, software or subscriptions.

    Computer Keyes has been developing Software Solutions since 1978!

    www.computerkeyes.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Get Your Security Education, And Not From The School Of Hard Knocks IBM Puts The Finishing Touches On PowerHA For IBM i 7.5

    Leave a Reply Cancel reply

TFH Volume: 32 Issue: 55

This Issue Sponsored By

  • Fresche Solutions
  • TL Ashford
  • ProData
  • ARCAD Software
  • Raz-Lee Security
  • WorksRight Software

Table of Contents

  • IBM’s Competitive Analysis For Power10 Entry Machines
  • Power10 Midrange Machine: The Power E1050
  • IBM Puts The Finishing Touches On PowerHA For IBM i 7.5
  • Guru: Regular Expressions, Part 2
  • Get Your Security Education, And Not From The School Of Hard Knocks
  • IBM i PTF Guide, Volume 24, Number 33

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • With Power11, Power Systems “Go To Eleven”
  • With Subscription Price, IBM i P20 And P30 Tiers Get Bigger Bundles
  • Izzi Buys CNX, Eyes Valence Port To System Z
  • IBM i Shops “Attacking” Security Concerns, Study Shows
  • IBM i PTF Guide, Volume 27, Number 26
  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle