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

    DevSecOps & Peer Review – The Power of Automation

    In today’s fast-paced development environments, security can no longer be an afterthought. This session will explore how DevSecOps brings security into every phase of the DevOps lifecycle—early, consistently, and effectively.

    In this session, you’ll discover:

    • What DevSecOps is and why it matters?
    • Learn how to formalize your security concerns into a repeatable process
    • Discover the power of automation through pull requests, approval workflows, segregation of duties, peer review, and more—ensuring your data and production environments are protected without slowing down delivery.

    Whether you’re just getting started or looking to enhance your practices, this session will provide actionable insights to strengthen your security posture through automation and team alignment to bring consistency to the process.

    Watch Now!

    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

  • What You Will Find In IBM i 7.6 TR1 and IBM i 7.5 TR7
  • Three Things For IBM i Shops To Consider About DevSecOps
  • Big Blue Converges IBM i RPG And System Z COBOL Code Assistants Into “Project Bob”
  • As I See It: Retirement Challenges
  • IBM i PTF Guide, Volume 27, Number 41
  • Stacking Up Power11 Entry Server Performance To Older Iron
  • Big Blue Boosts IBM i Support In Instana, Adds Tracing
  • It Is Time To Tell Us What You Are Thinking And Doing
  • IBM i PTF Guide, Volume 27, Number 40
  • The GenAI Boom Is Only Slightly Louder Than The Dot Com Boom

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