Search code examples
regexregex-group

Capture groups in 1 line with fixed delimiters


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!


Solution

  • 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.