0

I'm trying to do a regex expression in powershell to get only a specific part of a string. I know a way I can do this without regex but it can definitely be more efficient with. I have a string that looks like this:

Some/Stuff/Here/Then.drop.last

Ideally, I want to write a regex that gets me just:

Then.Drop
JayDee
  • 45
  • 8

1 Answers1

3
PS> 'Some/Stuff/Here/Then.drop.last' -replace '.*/(.+)\..*', '$1'
Then.drop
  • .*/ greedily matches everything up to the last /

  • (.+)\. greedily matches everything up to the last literal . and captures everything before that . in the first capture group ($1) - which is your string of interest.

  • .* matches the remaining part of the string.

  • Using $1 as the replacement string then replaces the overall match - the entire input string - with what the first capture group matched.

For more information about PowerShell's -replace operator, see this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775