Replacing text within a long URL using regex - regex

I am trying to setup a htaccess redirect, but I need some help with the writing regex expression. Here s what I am trying to get to:
Before: example.com/[wildcard1]/9-3/[wildcard2]/ref/[wildcard3]/[wildcard4]
After: example.com/[wildcard1]/wis/9-3/[wildcard2]/ref/[wildcard3]/[wildcard4]

Try Regex: ^([^\/]+)(\/9-3\/[^\/]+\/ref\/[^\/]+\/.+)$
Demo

Related

Multiple slash in URL replacement though regex

I am trying to create a regex in pcre, that is going to salinize URL with multiple slashes like the following:
https://www.domin.com/test1/////test2/somemoretests_67142 https://www.domin.com/test1/test2/somemoretests_67142///// https://www.domin.com/test1/test2///somemoretests_67142
So that I can replace it with the following: https://\2\4 and the link at the end of it looks: https://www.domin.com/test1/test2/somemoretests_67142
I have been struggling with it for the past couple of days, so any regex guru help is more than welcome :)
I have tried the following and more:
(http|https):\/\/(.*)(\/\/+)(.*)
(http|https):\/\/(.*)(\/\/){2,}(.*)
(http|https):\/\/(.*)(\/\/{2})(.*)
I am going to utilize these for Akamai to sanitize our URLs though cloudlet.
You can try:
(?<!https:\/)(?<!http:\/)(\/+$|(?<=\/)\/+)
And substitute the first group with empty string.
Regex demo.
This will produce this output:
https://www.domin.com/test1/test2/somemoretests_67142
https://www.domin.com/test1/test2/somemoretests_67142
https://www.domin.com/test1/test2/somemoretests_67142

How to replace part of a URL with regex

I need to remove part of a URL with a regex.
From the words: http or https to the word .com.
And it can be several times in one string.
Can anyone help me with this?
For example a string:
"The request is:https://stackoverflow.com/questions"
After the removal - "The request is:/questions"
The regex that performed the deletion perfectly is: (#"\w+://[^/$]*")
with replace "".
Something like that:
var regex = new Regex(#"\w+:\/\/[^\/$]*");
regex.Replace(url, "");
You can use the re.sub() function from the regex package. Alternatively if your working with python you can use urlparse package to extract different parts of the url and concatenate it to the prefix you want.

Replace characters in url by regex

I'm totally new to regex,
I'm using Yoast SEO - Redirects in wordpress, How to do that?
How to Replace "-and-" by "-" in url by regex
For example:
wwww.website.com/top-products-and-brands/product1/
To:
wwww.website.com/top-products-brands/product1/
I need to know what is the regex for match -and-
And how to redirect to the new link?
Thanks a lot.
Disclaimer: I have never used Yoast SEO
I think this will work:
Regular Expression:
(.*)(?:-and-)(.*)
New Url:
\$1-\$2
But honestly, I couldn't tell you because their docs on regex don't specify the syntax they use for capture groups, (or if they even support them at all).

Regular expression groups

For all the regex experts out there! I'm trying to figure out how to group my url into parts using regular expressions.
Example:
site.com/user/account/info/settings
I want to be able to capture the user/accout/info url NOT /settings
Can anyone take this challenge and be kind enough to help me out? Thanks!
If you want to get the beginning of the URL try this:
(\/.*\/(?!.*\/.+))
Input:
site.com/foo/remove-me/
site.com/user/account/info/settings
site.com/foo/bar/remove-me
site.com/foo/remove-me?param1=true&param2=hello+world
Output:
/foo/
/user/account/info/
/foo/bar/
/foo/
https://regex101.com/r/yI5rG4/2
After consideration of all your comments under your post, I understand that you want to get the last segment for controller name extraction. Hence try this:
(?:\/(?!.*\/.+))([^\?\n]*)
Used on these inputs:
site.com/foo/remove-me/
site.com/user/account/info/settings
site.com/foo/bar/remove-me
site.com/foo/remove-me?param1=true&param2=hello+world
Output for group 1:
remove-me/
settings
remove-me
remove-me
Test here: https://regex101.com/r/kR5tX6/2

How can I make this regex for a URL more specific?

I have the following regex that attempts to match URLs:
/((http|https):(([A-Za-z0-9$_.+!*(),;/?:#&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:#&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))/g
How can I modify this regex to only match URLs of a single domain?
For example, I only want to match URLs that begin with http://www.google.com?
This should simplify my regex, but I'm too much of a regex noob to get it working (after all these years...)
Did you write that RegEx? I don't know what it's trying to do, but it certainly doesn't match URLs correctly. Here's something it matches:
http:###9#?~
which I'm pretty sure isn't a valid URL.
You shouldn't be using RegEx to match URLs like this. You haven't said what language you're working in, but use whatever its equivalent of urlparse is..
Here's a relevant question: How do you validate a URL with a regular expression in Python?