-1

With the git log command, I get all changes from a commit and I store it in a file.

This is de result:

microsoft/netframework/docker/content/Y

I want to get only the third folder, docker.

I tried with these regex expression: '/[\/]*[^\/]+[\/]([^\/]+)/'

But it returns:

microsoftcontent/Y

What should I correct in the expression to use with Powershell?

  • 1
    Which language/tool are you using? It affects what possible solutions there are. – Bohemian Feb 20 '18 at 22:49
  • I updated it, I use powershell – Steve Mason Feb 20 '18 at 22:52
  • Answering that became too awkward, because now you've invalidated a perfectly fine answer by GillesQuenot. However, [this here](http://regexstorm.net/tester?p=%5e%28%3f%3a%5cw%2b%5c%2f%29%7b2%7d%28%5cw%2b%29&i=microsoft%2fnetframework%2fdocker%2fcontent%2fY) should work in powershell. – Andrey Tyukin Feb 20 '18 at 22:54
  • @AndreyTyukin It returns `microsoft/netframework/docker` not only `docker` folder. – Steve Mason Feb 20 '18 at 22:58
  • Try `$s -replace '^(?:[^/]*/){2}([^/]+).*', '$1'` (Powershell). Or try a matching pattern - `(?<=^(?:[^/]*/){2})[^/]+` – Wiktor Stribiżew Feb 20 '18 at 22:59
  • @SteveMason it matches "docker" as the first matching group. Are you in a situation where you don't even have access to "$1"? – Andrey Tyukin Feb 20 '18 at 22:59
  • @SteveMason I updated the answer with patterns for both matching and replacing. Since powershell [doesn't seem to support \K](https://stackoverflow.com/questions/46611602/what-is-the-equivalent-to-k-for-regex-in-powershell), it doesn't seem to be possible to do it without replacement. – Andrey Tyukin Feb 20 '18 at 23:33
  • @AndreyTyukin you can easily [handle it without any replacement](https://imgur.com/a/QaZ2V), see `$matches[0]`. Actually, you may use capturing that OP is so afraid of and access it via `$matches[1]`. – Wiktor Stribiżew Feb 20 '18 at 23:39
  • @WiktorStribiżew I don't know anything about powershell. But every not-self-written regex engine that I've encountered so far allowed to access the matched groups in one way or the other. If this is how it can be done in powershell: good, I believe it. In this case the OP is "not in a situation where [he doesn't] even have access to $1". If accessing matched groups is so simple, then I don't know what the problem with my first solution was. The online regex-testing utility provided both a table of matched groups *and* an input field for replacement. Now my example contains both possibilities. – Andrey Tyukin Feb 20 '18 at 23:43

2 Answers2

0

Check online https://regex101.com/r/8ELSUx/2

^(?:\w+\/){2}\K\w+

You have all explanations on the right panel

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • That regular expression is not valid with Powershell. I'm sorry I did not add that point. I just updated the post. – Steve Mason Feb 20 '18 at 22:50
0

Even in powershell, you should have the possibility to select single groups of the matched pattern.

This regex in combination with replacement pattern matches the entire string, but retains only docker after replacement (click on "Context" tab).

Regex:

^(?:\w+\/){2}(\w+)(\/.*)

Replacement:

$1

The first group isn't matched because of :?. The last group is simply not used in the replacement.

Andrey Tyukin
  • 43,673
  • 4
  • 57
  • 93