• 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
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.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

  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23
  • SEU’s Fate, An IBM i V8, And The Odds Of A Power13
  • Tandberg Bankruptcy Leaves A Hole In IBM Power Storage
  • RPG Code Generation And The Agentic Future Of IBM i
  • A Bunch Of IBM i-Power Systems Things To Be Aware Of
  • IBM i PTF Guide, Volume 27, Numbers 21 And 22

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