1

I'm trying to create regex to match the path of the file, excluding the file itself and drive.

c:\foo\bar\baz.txt
C:\FOO\BAR\BAZ.TXT
C:\FoO\BaR\BaZ.TxT
and so on.

Expression should match `foo\bar` (IN any CaSe).

The basic example I have is

\\.*\\

Another example is

\\.*(?=\\)

But I'm not sure I'm on the right way.

Live version is here: https://regexr.com/4kmu0

john c. j.
  • 725
  • 5
  • 28
  • 81
  • 1
    What language/tool are you using? – Sweeper Sep 10 '19 at 06:23
  • One option could be using a capturing group `^[^\\\r\n]*((?:\\[^\\\r\n]+)+)\\[^\\\r\n]+$` https://regex101.com/r/ycCqYd/1 – The fourth bird Sep 10 '19 at 06:25
  • 1
    `^\w+:\\(.+)\\[^\\]*$` and get group 1? – Sweeper Sep 10 '19 at 06:25
  • @Sweeper Well, I need it for AutoHotkey. The live demo is here: https://regexr.com/4kmu0. In case it works in demo, it will work in AutoHotkey as well. – john c. j. Sep 10 '19 at 06:26
  • 2
    Basically a duplicate of https://stackoverflow.com/questions/169008/regex-for-parsing-directory-and-filename since the solution is the same, but adding (or replacing with) backslash and adding the drive: group 1 of `^(?:.:\\)(.+)(?:/|\\)(?:[^/\\]+)$` – Grismar Sep 10 '19 at 06:26
  • Well, both published answers (by _Vineet_ and _Michał Turczyn_) works - and I have upvoted them. However, the comments here by _Sweeper_ and _Grismar_ seems to work better. The version from _The fourth bird_ leaves the first slash. Probably I could remove it a bit later. – john c. j. Sep 10 '19 at 06:48
  • If you accept starting and ending with slashes, `/((?:\\\w+)+\\)/` – Taha Paksu Sep 10 '19 at 07:14
  • @johnc.j. You are right, I have added an answer with an updated pattern. – The fourth bird Sep 10 '19 at 07:16
  • @Thefourthbird Thanks. Yes, I see, you posted the answer with explanation. But the pattern itself is the same: `^[^\\\r\n]*((?:\\[^\\\r\n]+)+)\\[^\\\r\n]+$`. Isn'it? – john c. j. Sep 10 '19 at 07:37
  • @johnc.j. My bad, I posted the old one. Updated to the new pattern. – The fourth bird Sep 10 '19 at 07:47

5 Answers5

2

Try ^[^\\]+\\(.+)\\

Explanation:

^ - beginning of a string

[^\\]+ - match one or more chcaracters other than \

(.+) - match one or more of any charactes and store it in capturing group

\\ - match \ literally

Demo

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
1

\([a-z].*)\

Above expression will match exactly foo\bar with Group 1.

  • Thanks. Interesting. It is very short and simple, but there quite a lot very complex variations here. Nice, but suspicious. By the way, it should be slightly changed: at least, to the `\\\([aA-z].*)\\\`. Demo is here: https://regexr.com/4kn13 – john c. j. Sep 10 '19 at 07:16
  • Are you sure? Try it with `c:\foo\bar\blah\blah\baz.txt` – Toto Sep 10 '19 at 12:30
1

If the delimiter of the path should be a single backslash, you could use a capturing group and repeat matching \ followed by matching 1+ times not \

^[^\\\r\n]*\\([^\\\r\n]+(?:\\[^\\\r\n]+)+)\\[^\\\r\n]+$
  • ^ Start of string
  • [^\\\r\n]*\\ Match not \ or newline, then match \
  • ( Capture group 1
    • [^\\\r\n]+ Match 1+ times not \
    • (?: Non capturing group
      • \\[^\\\r\n]+ Match \, then 1+ times not \ or newline
    • )+ Close group and repeat 1+ times
  • ) Close group 1
  • \\[^\\\r\n]+ match \, then 1+ times not \ or newline
  • $ End of string

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

The following will do:

:\\(\S*)\\ /gi

This captures all non-whitespace characters which appear in between :\ and the last \. The captured group then contains the required text.

Note the usage of /i flag for case-insensitivity.

Demo

CinCout
  • 9,486
  • 12
  • 49
  • 67
0

Try

(?>[a-zA-Z]:\\)(.*)(?>\\.+\..+)
  1. (?>[a-zA-Z]:\) : will match c:\ or d:\ ,this is non capturing group
  2. (.*) will match anything in between
  3. (?>\.+..+), this is again non-capturing group, match baz.txt or file.txt

Demo

Vineet
  • 44
  • 7