Help with regex pattern

Hello Makers, I cannot wrap my head around the usage of REGEX.

Here are five input examples, I bolded what I would like extract:

  • VTOAL232, remaining part of invoice
  • ZAGOR2303-JS John Smith
  • ACTDA2302/balance due
  • Deposit for booking reference ALPDA231
  • Invoice: ALPDA2309-MO

The pattern I’m trying to extract is positioned anywhere in the string and consists of 5 letters followed by 3 OR 4 numbers, potentially with a dash and 2 more letters as an optional extension. Any advise is welcome. :slight_smile:

ChatGPT is usually good at creating Regex patterns.

But it does sometimes get them wrong, so it’s often best to validate them using the Regex checker and debugger at https://regex101.com/ (be sure to select ECMAScript in the left-hand menu).

2 Likes

Something like this will work,

[A-Za-z]{5}\d{3,4}(?:-[\w]{2})?

For the last part, it will pick up any word character, if you want you can use A-Za-z instead of \w over there to ignore numbers.

3 Likes

Awesome, thank you!

I’m working on understanding how this works…

  • d{3,4} ← that’s great, I didn’t realize you can have an OR.
  • (?:-)? ← gotta play around with this one to understand it. :-7
1 Like

@Runcorn could I ask you once more for your expertise with Regex?

I’m trying to achieve the following

  • String: “2023-06-25-Vila 60-Host|Andi: 4x SGL, 4x DBL (B&B)”
  • Match: “2023-06-25-Vila 60-Host”
  • Extract: “Andi: 4x SGL, 4x DBL (B&B)”

Here’s an example roll-up (with comma delimiter) for a bunch of those strings:

2023-06-09-Vila 60-Host|FIT: 1x DBL (B&B),2023-06-18-Vila 60-Host|FIT: 1x DBL (B&B),2023-06-25-Vila 60-Host|FIT: 1x DBL (B&B),2023-06-22-Vila 60-Host|FIT: 1x DBL (B&B),2023-05-25-Vila 60-Host|FIT: 1x DBL (B&B),2023-07-17-Vila 60-Host|FIT: 1x DBL (B&B),2023-07-08-Vila 60-Host|FIT: 1x TWIN, 1x TPL (B&B),2023-07-03-Vila 60-Host|FIT: 1x DBL (B&B),2023-06-25-Vila 60-Host|FIT: 1x DBL (B&B),2023-06-28-Vila 60-Host|FIT: 1x DBL (B&B),2023-05-22-Vila 60-Host|FIT: 1x DBL (B&B),2023-06-25-Vila 60-Host|Andi: 4x SGL, 4x DBL (B&B),2023-07-01-Vila 60-Host|Andi: 4x SGL, 4x DBL (B&B),2023-07-03-Vila 60-Host|FIT: 1x TPL (B&B),

With the following three matches and the desired string to be extracted in bold.

  • 2023-06-25-Vila 60-Host|FIT: 1x DBL (B&B),
  • 2023-06-25-Vila 60-Host|FIT: 1x DBL (B&B),
  • 2023-06-25-Vila 60-Host|Andi: 4x SGL, 4x DBL (B&B),

I’m hinting on something like this, but find it difficult to complile the entire string

(?<=[|]).*(?=[,])

Hi @Zbulo,

I have generalized the regex to get anything between pipe and comma, not sure if that is what you desired for.

|([^,]+)

What this does is it will grab anything in between a pipe and comma, Let me know if it is what you want, we can also incorporate the 2023-06-25-Vila 60-Host for better result, but I am not sure how it looks for other results beside the one you shared.