nflgreaternba
nflgreaternba

Reputation: 73

Match multiple strings in the same text with a single regex

I have the following regex entry

(?<Nr>3\d{6})(?=[\s a-zA-Z\r\n]|$)

to search for a specific sequence of numbers in a text (starting with "3" and followed by 6 other digits). I need the regex for Blueprism, where True is returned if the sequence exists, and False if not.

Now, I would like to modify the regex so that only True is returned if this sequence of numbers and also the word "Invoice" occurs in the text (no matter where). And this should be done with only one regex because the tool doesn't accept several regular expressions.

This is an example of input text:

Company XYZ
Max Mustermann
Robert-Koch-Str. 12
12345 Musterstadt Tax number
Institute
IBAN BIC 123/456/78901
DAB-Bank Glasses
DE00 3006 0088 1234 5678 90
DRTTZZUUXXX
Invoice number:
3222222
Sample city, DD.MM.YYYY
Invoice
Dear Sir or Madam
You hereby receive the invoice for the service I have provided. Please pay the total amount listed below, stating the invoice number, by DD.MM.YYYY to the account indicated.
Description Price/hour Number Amount (Euro)
Sample service 1 30.00 4 120.00
Sample service 2 36.00 7 252.00
Total services 372.00
Discount -20.00
Total amount 352.00
The amount shown does not include VAT in accordance with § 19 UStG.
Yours sincerely
MyFirstName MyLastName

Is it possible to achieve that with only one regex? How could I do that?

Upvotes: -1

Views: 61

Answers (1)

Chris Maurer
Chris Maurer

Reputation: 2615

You can use a lookahead for 'invoice' like this, (?=.*?invoice) but you also need to turn on the single-line and case insensitive flags (s and i) with (?si)(?=.*?invoice).

The fully altered regex is (?si)(?=.*?invoice)(?<Nr>3\d{6})(?!\d).

I took the liberty of simplifying your original by turning your lookahead into a negative lookahead to simply require a "not digit" after the 6 digits. This worked just fine on Regex101.

Upvotes: 1

Related Questions