Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 449 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular Expression, remove everything after last forward slash

#1
I'm trying to use a regular expression within PowerShell to remove everything from the last slash in this string;

NorthWind.ac.uk/Users/Current/IT/Surname, FirstName
NorthWind.ac.uk/Users/Dormant/DifferentArea/Surname, FirstName

I need to remove Surname, FirstName including the /.
The string should look like this.

NorthWind.ac.uk/Users/Current/IT

If someone could help me, I would be very grateful.

I have tried this; ` -replace '([/])$',''` but I can't seem to get it to work.

Thanks
Reply

#2
Replace `/[^/]*$` with an empty string
Reply

#3
check this regex

[To see links please register here]

?2vhll
i can't test it on powershell but it work in the regex generator

/(?!.*/).*
Reply

#4
Here's a solution that doesn't require regular expressions:

PS> $cn = 'NorthWind.ac.uk/Users/Current/IT/Surname, FirstName' -split '/'
PS> $cn[0..($cn.count-2)] -join '/'
NorthWind.ac.uk/Users/Current/IT
Reply

#5
Here's another solution that doesn't require regular expressions:

Take a substring of your string starting at the beginning of the string and ending before the index of the last slash in your string:

PS> $indexoflastslash = ("NorthWind.ac.uk/Users/Current/IT/Surname, FirstName").lastindexof('/')
PS> "NorthWind.ac.uk/Users/Current/IT/Surname, FirstName".substring(0,$indexoflastslash)



Reply

#6
This solution doesn't use regexes.
I believe this approach is probably easier to understand, after all regexes - in general - are hard to read:

`NorthWind.ac.uk/Users/Current/IT/Surname, FirstName` has a path-like structure (windows also supports the forward slash as a path separator), so we could use split-path to return the parent 'directory' path.

Because '\' is the default path separator, we need to replace the '\' with '/' after doing this:

(split-path NorthWind.ac.uk/Users/Current/IT/Surname, FirstName).replace('\','/')
# will return NorthWind.ac.uk/Users/Current/IT

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through