Problem: You need a working regex to delete an /unwanted section/ of a URL, on a self-hosted installation of WordPress. The /unwanted section/ is different in each URL, and you have hundreds or even thousands of such URLs to deal with.

Solution: If your /unwanted section/ string has a repeating section of the URL in front of it, then you’re in luck. The popular free WordPress plugin ‘Search Regex’ and this small regex, will do it. The regex is seen here working as expected in the RegularExpressions101 sandbox…

What it’s doing: The desired repeating URLpart2 in the URL path is being found and marked, along with its following / slash. This and whatever follows the found section is also marked for deletion, even if what follows is different in every URL. The ‘marking for deletion’ stops at the next / in the URL.

In WordPress: Here’s how it’s applied in Search Regex in WordPress. Because this example is also deleting the repeating URLpart2/, that gets added back via the “Replace” box.

Warnings: Always preview first, using “Search”. Only if you are absolutely happy with how the URL now looks in the previews, should you then press “Replace All”. Always make a backup of the WordPress install before attempting such changes.