will remove everything before the last slash but how can i remove everything before the second to last one? How did adding new pages to a US passport use to work? By clicking Post Your Answer, you agree to our terms of service, privacy policy and cookie policy. Can I change which outlet on a circuit has the GFCI reset switch? Site design / logo 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA. This is a requirement by, Of course I know that. rev2023.1.17.43168. Would Marx consider salary workers to be members of the proleteriat? . By clicking Post Your Answer, you agree to our terms of service, privacy policy and cookie policy. How can I remove a specific item from an array? EditSince youre using PHP, you could also use strrchr thats returning everything from the last occurence of a character in a string up to the end. The best answers are voted up and rise to the top, Not the answer you're looking for? I'm extracting an ID used by Google's GData. Is every feature of the universe logically necessary? I tried, $s = explode("/",$str); Make "quantile" classification with an expression. I'm working in PHP with friendly URL paths in the form of: I need to standardize these URL paths to remove anything after the 4 slash including the slash itself. MathJax reference. P.S. Why is 51.8 inclination standard for Soyuz? @angel0smile thank users who answer your question by upvoting (when you're able), and selecting their answer: In R, how to remove everything before the last slash, Microsoft Azure joins Collectives on Stack Overflow. or 'runway threshold bar? this finds /one.txt and replaces it with /. Joachim Isaksson Jan 9, 2012 at 15:30 Add a comment 4 The data you want would then be the match of the first group. Edit Since youre using PHP, you could also use strrchr th Making statements based on opinion; back them up with references or personal experience. Did Richard Feynman say that anyone who claims to understand quantum physics is lying or crazy? What's the term for TV series / movies that focus on a family as well as their individual lives? @KyleMit Cant you see? Is this variant of Exact Path Length Problem easy or NP Complete. A dot is the correct directory to find a file whose path is specified with no path component. Since this is regularly proving useful for others, here's a snippet example As stated in @Archer's answer, you need to double up on the backslashes. i.e. Regular Expression, remove everything after last forward slash, Microsoft Azure joins Collectives on Stack Overflow. and let a line with no slashes pass through unchanged, i can't test it on powershell but it work in the regex generator. Can state or city police officers enforce the FCC regulations? To subscribe to this RSS feed, copy and paste this URL into your RSS reader. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. 31,633. How to trim string start from back until dash? The following example identifies duplicate words in a string and uses the $+ substitution to replace them with a single occurrence of the word. Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide. Here's a solution that doesn't require regular expressions: 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: This solution doesn't use regexes. How to translate the names of the Proto-Indo-European gods and goddesses into Latin? Microsoft Azure joins Collectives on Stack Overflow. (. Browse other questions tagged, Where developers & technologists share private knowledge with coworkers, Reach developers & technologists worldwide. Meaning of "starred roof" in "Appointment With Love" by Sulamith Ish-kishor. How to remove up to a certain punctuation when there are same punctuations. Thank you so much. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. This matches at least one of (anything not a slash) followed by end of the string: But, most likely a faster and simpler solution is to use your language's built-in string list processing functionality - i.e. so the desired output for the above is: Caveat: an input line that contains no / characters Is the rarity of dental sounds explained by babies not immediately having teeth? @Martijn I understand completely. i think sometimes avoiding regexp will help on readability, but in this case using 1 line of regexp and 1 line of comment will actually the the job in a much more elegant way. Stack Exchange network consists of 181 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Super User is a question and answer site for computer enthusiasts and power users. it is working on https://regex101.com/ for all (PCRE (PHP), ECMAScript, (JavaScript), Python, Golang) but it Thanks for contributing an answer to Stack Overflow! Detailed match information will be displayed here automatically. Strange fan/light switch wiring - what in the world am I looking at. I don't know if my step-son hates me, is scared of me, or likes me? based on @ Mark Rushakoff 's answer the best solution for different cases: