RegEx Workout 001 - Introduction to Regex

Welcome to first Regex Workout.

The purpose of this exercise is to detect fragments of the given text that meet the requirements.

Our primary tool will be regex101.com. Let’s take a moment to get to know its interface:

The key elements of the tool are two windows. The smaller one is used to enter a regular expression. In the larger one you type the text you are analyzing.

On the right are the Explanation and Match Information panels. The first one tries to interpret our expression and the second one displays the identified elements.

Quick reference is knowledge base about regular expressions.

image

First challenge

Text to analyze:

“Thank you for paying the invoices INVOICE-2023123 and INVOICE-2021133, and we would also like to remind you to pay the invoices INVOICE-202023555 and INVOICE-20201455. We would also like to inform you that we are processing your credit note CR-202345345. We look forward to a successful cooperation in 2023.”

Tasks:

Level: Easy

Match first sign:

Match last sign:

Match all digits :

Level: Medium
Find all invoices:

Find all invoices but have the text part and special characters in one group and the numbers in a separate group.

image

Hard:

Match all financial documents, including “CR-XXX” . We will define them as every numeric value followed by uppercase string and “-“.

image

Submission

Simply post your code and a screenshot of your results.

Please format your Regex code and blur it or place it in a hidden section.

Solution to this challenge will be posted on May 21

3 Likes

Medium+Hard

([A-Z]+)-(\d+)

2 Likes

@KrzysztofNowak ,

Terrific intro workout! I’d never used Regex101 previously, but it’s a great tool.

Already looking forward to Workout #2. Thanks!

  • Brian

EASY:

Summary

MEDIUM:

Summary

HARD:

Summary

1 Like

Hello All, thank you for participation.

Below is my solution

  1. Find first character. I decided to use “^” anchor which indicated start of line. Second value is “.” which means any character.

  2. Find last character. I changed anchor from “^” to “$” and placed it after “.”. It means, look at the end of line.

3.Match all digits. In this case I use “\d” which means “digit”. We need "" as escape character. This way the tool knows that we do not mean the letter “d”.

4.Find all invoices. There is a few ways to complete this. Most direct is to mention “INVOICES-” and digits (one or more with “+”)

We could have more dynamic solution and say: At least 3 upper case letters(to avoid matching “CR”), non-word sign and digits (one or more).

In order to include “CR” in last question, change value in {} to 2

Regards

2 Likes

Good job, that You for submission @borydobon .

1 Like

Hello @BrianJ , thank You for submission. All your answers are right. For last exercise
I decided to be a little more careful and avoid matching letter prefix shorter but 2, but results are the same.

1 Like

A little late to the party, but here’s my workout solution!
Didn’t used Regex for 2 years. It was fun!

Level: Easy

Summary

Match first sign: ^.
Match last sign: .$
Match all digits: \d

Level: Medium & Hard

Summary

Find all invoices: (INVOICE).(\d+)
Match all financial documents, including “CR-XXX” : [A-Z]±\d+

2 Likes

@KrzysztofNowak ,

Thanks! I’m really gaining an appreciation for the art and science of regex through these workouts.

It’s not just about getting the specific answer to the problem provided correct, but thinking about what else COULD go wrong with the data and proactively coming up with constructs that will successfully handle it.

Great stuff!

Hi @KrzysztofNowak .

First time using this. :slight_smile:

Level Easy:
Match First Sign :

Match Last sign:

Match All Digits:

Level: Medium
Find All Invoices

Find all invoices but have the text part and special characters in one group and the numbers in a separate group.

Hard Solution:
Match all financial documents, including “CR-XXX” . We will define them as every numeric value followed by uppercase string and “-“.

Thanks for the opportunity,
Keith

1 Like