Page 1 of 1

Add space after a subdomain in an url. How?

Posted: 17 Mar 2019, 21:04
by KilliK
Hello.
Let's say I have thesel urls:

Code: Select all

https://www.test.com/
http://test.co.uk/jpg.png
https://pbs.test.gr/xxx/xxx.jpg
How do I add a space between the subdomain ( test in this case) and the . that follows it, so that they look like this:

Code: Select all

https://www.test .com/
http://test .co.uk/jpg.png
https://pbs.test .gr/xxx/xxx.jpg
I am trying to do it with StrReplace but with no results. I think I need to use RegexMatch but I dont know how to do it.

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 00:45
by IMEime
I hate rude people

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 06:26
by KilliK
But this only works if you already know the subdomain. Maybe I wasnt clear, but the subdomain is supposed to be random from random urls.

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 06:44
by teadrinker
Hi

Code: Select all

urls =
(
https://www.test.com/
http://test.co.uk/jpg.png
https://pbs.test.gr/xxx/xxx.jpg
)

MsgBox, % RegExReplace(urls, "m`a)^https?://(?:www.)?\K(.+?)(?=\.\w+/)", "$1 ")

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 06:59
by KilliK
you are magnificent, thank you very much.

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 08:01
by IMEime
your attitude is the magnificent....

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 08:24
by teadrinker
Easier:

Code: Select all

RegExReplace(urls, "m`a)^(https?://.+?)(?=\.\w+/)", "$1 ")

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 08:41
by sinkfaze
@IMEime

You're about to take another vacation from these forums. Cool it.

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 08:55
by IMEime
@sinfaze
yes
I am considering a month or two off

Thanks for the kind tip

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 10:06
by KilliK
teadrinker wrote:
18 Mar 2019, 08:24
Easier:

Code: Select all

RegExReplace(urls, "m`a)^(https?://.+?)(?=\.\w+/)", "$1 ")
thanx, but I just noticed something which eluded me the first time. The space is not added correctly to the first dot after the subdomain name, if the domain has two dots.

for example, these urls:

Code: Select all

http://www.test.co.uk/jpg.png
http://test.co.uk/jpg.png
give:

Code: Select all

http://www.test.co .uk/jpg.png
http://test.co .uk/jpg.png
where it should be:

Code: Select all

http://www.test .co.uk/jpg.png
http://test .co.uk/jpg.png
This happens with both instaces of your code.

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 10:37
by sinkfaze
KilliK wrote:
17 Mar 2019, 21:04
Hello.
Let's say I have thesel urls:

Code: Select all

https://www.test.com/
http://test.co.uk/jpg.png
https://pbs.test.gr/xxx/xxx.jpg
How do I add a space between the subdomain ( test in this case) and the . that follows it, so that they look like this:

Code: Select all

https://www.test .com/
http://test .co.uk/jpg.png
https://pbs.test .gr/xxx/xxx.jpg
I am trying to do it with StrReplace but with no results. I think I need to use RegexMatch but I dont know how to do it.
Only one of your above examples has what I would think of as a subdomain, https://pbs.test.gr/xxx/xxx.jpg. It looks more like you're trying to split the second/top level domains from the domain name?

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 10:52
by teadrinker
KilliK wrote: that they look like this:

Code: Select all

https://pbs.test .gr/xxx/xxx.jpg
KilliK wrote: where it should be

Code: Select all

http://www.test .co.uk/jpg.png
How the script can know what the variant is true? Why not like this: https://pbs .test.gr/xxx/xxx.jpg ?

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 11:25
by KilliK
teadrinker wrote:
18 Mar 2019, 10:52
....
you are right. and i think turning all the dots between // and the first / will suffice for my situation.

Let me explain the problem. Some Disqus forums dont allow urls in your posts. Otherwise they will auto-delete them. But sometimes I need to add urls for reference during my conversations. After some testing, the best way to circumvent this, is to add spaces in the domain/subdomain part of the url. I want to do this automatically with AHK, by turning this:

Code: Select all

https://a.test.b.c/x/zzz.jpg
into this

Code: Select all

https:// a .test .b .c/x/zzz.jpg
I tried to use StrReplace, but it adds spaces to all the dots in the url. That's why I thought that RegExReplace must be a better solution and I came here for help, since regex is a nightmare for me to handle. :oops:

Re: Add space after a subdomain in an url. How?  Topic is solved

Posted: 18 Mar 2019, 13:47
by teadrinker
If you need to change only one url:

Code: Select all

url = https://a.test.b.c/x/zzz.jpg
MsgBox, % RegExReplace(url, "\.(?!\w+$)|(?<=//).", " $0")
; or
MsgBox, % RegExReplace(url, "[^/:](?=/)(*COMMIT)(*FAIL)|(\.|(?<=//).)", " $1")
If all together at once, I did not find one-string solution.

Code: Select all

urls =
(
https://www.test.com/
http://test.co.uk/jpg.png
https://pbs.test.gr/xxx/xxx.jpg
)
Loop, parse, urls, `n, `r
   newUrls .= RegExReplace(A_LoopField, "[^/:](?=/)(*COMMIT)(*FAIL)|(\.|(?<=//).)", " $1") . "`r`n"
MsgBox, % RTrim(newUrls, "`n`r")

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 16:47
by KilliK
Your code is excellent, it does the job perfectly. thank you again for all your help.

Re: Add space after a subdomain in an url. How?

Posted: 18 Mar 2019, 18:23
by electrone77
hey, here's my 2 cents. kinda :offtopic: since question is already answered :angel:

Code: Select all

urls =
(
https www.test.com /  Broken Link for safety
http test.co.uk /jpg.png  Broken Link for safety
https pbs.test.gr /xxx/xxx.jpg  Broken Link for safety
)

msgbox % regexreplace(urls, "m`a)(https?://(www\.|pbs\.)?.*?)\.","$1 .")
or if you encounter other than www/pbs (has to be 3 letters but won't work if the domain name is also 3 letters):

Code: Select all

regexreplace(urls, "m`a)(https?://(\w{3}\.)?.*?)\.","$1 .")