Home > Mobile >  RegEx - Match a String Between Last '\' and Second '_'
RegEx - Match a String Between Last '\' and Second '_'

Time:10-19

I am trying to extract part of a filename out of a file path so that I can use it in the filename of a modified file. I'm having a little trouble trying to get RegEx to give me the part of the filename that I need, though. Here is the file path that I'm working with:

X:\\folder1\\folder2\\folder3\\folder4\\folder5\\Wherever-Place_2555025_Monthly-Report_202209150000.csv

Within this path, the drive name, the number of folders, the number of dashes in "Wherever-Place", and the information after the second underscore in the filename may vary. The important part is that I need to extract the following information:

Wherever-Place_2555025

from the path. Basically, I need to match everything between the last backslash and the second underscore. I can come up with the following RegEx to match everything after the last backslash:

[^\\] $

And, if I run the output of that first RegEx through this next RegEx, I can get a match that includes the beginning of the string through the last character before the second underscore:

[^_] _[^_] 

But, that also gives me another match that starts after the second underscore and goes through the end of the filename. This is not desirable - I need a single match, but I can't figure out how to get it to stop after it finds one match. I'd also really like to do all of this in one single RegEx, if that is possible. My RegEx has never been that good, and on top of that what I had is rusty...

Any help would be much appreciated.

CodePudding user response:

If Lookarounds are supported, you may use:

(?<=\\)[^\\_]*_[^\\_]*(?=_[^\\]*$)

Demo.

CodePudding user response:

For this match:

Basically, I need to match everything between the last backslash and the second underscore.

You can use a capture group:

.*\\\\([^\s_] _[^\s_] )

The pattern matches:

  • .*\\\\ Match the last occurrence of \\
  • ( Capture group 1
    • [^\s_] _[^\s_] Match 1 chars other than _ and \, then match the first _ and again match 1 chars other than _ and \
  • ) Close group 1

Regex demo

Or if supported with lookarounds and a match only:

(?<=\\)[^\s_\\] _[^\s_] (?![^\\]*\\)

The pattern matches:

  • (?<=\\) Positive lookbehind, assert \ to the left
  • [^\s_\\] _[^\s_] Match 1 chars other than _ and \, then match the first _ and again match 1 chars other than _ and \
  • (?![^\\]*\\) Negative lookahead, assert not \ to the right

Regex demo

  • Related