0

I'm a beginner at regex and still don't understand a lot. I apologize in advance from any wrong notations or missing information :(

I need to extract groups from an e-mail subject where I have to use each value further on in a process to use as a folder or document name.

Example: 123456/TEXT/567890/01Moretext

I need to get the following pieces of text:

123456
TEXT
567890
01Moretext

in seperate regex commands.

So far I have:

  1. ^\d{6}, which gives me 123456
  2. (?<=/)[^/]*, which gives me TEXT
  3. I can't figure out how to extract the third group, 567890
  4. [^/]*$, which gives me 01Moretext

Would appreciate any help that can prevent my head from exploding!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Cor neetje
  • 21
  • 3
  • 3
    Why not split on `/`? – The fourth bird Oct 10 '22 at 11:31
  • I thought I was doing that? I'm not even sure how to do this now and how to capture it in a regex...Like how to capture the first group and how then the second group.... – Cor neetje Oct 10 '22 at 11:40
  • 1
    The problem description sounds like you are using a language or tool which returns the whole match as "group 0" and the first parenthesized expression as group 1. Without seeing your code, we can only speculate. Please review the [tag:regex] tag description, which explains what you need to include in your question. – tripleee Oct 10 '22 at 11:40
  • I'm using the regex in a OCR scanning tool "Umango" where the regexes all start with REGEX(...) I don't know what to add otherwise.... – Cor neetje Oct 10 '22 at 11:51
  • Try writing the pattern as `/(?<=\/)[^\/]+|[^\/]+(?=\/)/gm` See https://regex101.com/r/oym1v0/1 – The fourth bird Oct 10 '22 at 12:52
  • I got 3 groups working: for group 1: ^\d{6} for group 2: (?<=/)[^/]* for group 4: [^/]*$ but i can't get the third group working. – Cor neetje Oct 10 '22 at 12:58
  • See `^\d{6}/[^/]*/\K[^/]+` [demo](https://regex101.com/r/oym1v0/2). If that does not work, try `[^/]+(?=/[^/]*$)` - see [this demo](https://regex101.com/r/oym1v0/3). – Wiktor Stribiżew Oct 10 '22 at 13:10

1 Answers1

0

You can use

[^/]+(?=/[^/]*$)

See the regex demo. Details:

  • [^/]+ - one or more chars other than /
  • (?=/[^/]*$) - a positive lookahead that requires a / and then one or more chars other than / till the end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563