It is currently October 19th, 2020, 9:30 pm

RegExpSubstitute Website name from URL

Help with creating, editing & fixing problems with skins
User avatar
StArL0rd84
Posts: 356
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

RegExpSubstitute Website name from URL

Post by StArL0rd84 »

I am making a launcher and I have made a measure that grabs the name from the exe or folder, but i also want to add websites and automatically grab the name from URL's too.
url.png
I know very little about how the RegExpSubstitute in the code below works, not enough to possibly add this function.
So my question finally Is...
Is it possible to form a RegExpSubstitute that also removes everything from a URL but the name of the website?

Code: Select all

[mObjectName1]
Measure=String
String=#Object1#
OnChangeAction=[!SetVariable ObjectName1 "[&mObjectName1]"][!WriteKeyValue Variables ObjectName1 "[&mObjectName1]"][!UpdateMeasure mClickObject1][!UpdateMeterGroup Object1][!Redraw]
RegExpSubstitute=1
Substitute="^(.*)\\([^\\]*)$":"\2",".exe":""
UpdateDivider=-1
DynamicVariables=1
Group=Object1
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 11575
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: RegExpSubstitute Website name from URL

Post by balala »

StArL0rd84 wrote: October 9th, 2020, 6:48 pm Is it possible to form a RegExpSubstitute that also removes everything from a URL but the name of the website?
Don't know. For instance from the https://www.rainmeter.net/ URL would you like to keep only rainmeter? Have I understood well?
If I am right, try something like this: Substitute="(.*)://(.*)\.(.*)\.(.*)":"\3"
User avatar
Yincognito
Posts: 2760
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: RegExpSubstitute Website name from URL

Post by Yincognito »

StArL0rd84 wrote: October 9th, 2020, 6:48 pm I am making a launcher and I have made a measure that grabs the name from the exe or folder, but i also want to add websites and automatically grab the name from URL's too.
url.png
I know very little about how the RegExpSubstitute in the code below works, not enough to possibly add this function.
So my question finally Is...
Is it possible to form a RegExpSubstitute that also removes everything from a URL but the name of the website?
Very few things (if any) are impossible, but it's true that this, although a very good question, is a tricky one. Tricky because the local path syntax and the URI syntax, while somewhat similar, are also different in terms of structure and characters acting as delimiters (simple example: the # character marks the beginning of a fragment in the URI, so a part that you wouldn't want to include in the host name result if they are adjacent, e.g. www.Rainmeter#Top, yet is a perfectly valid character to be included in a file name, e.g. C:\Rainmeter#2.exe; fortunately some of these cases are rare and URIs like www.rainmeter.net#Top work). That being said, I think the regex below is a decent attempt to solve the issue:

Code: Select all

[Variables]
StringCount=10
StringIndex=0
String0=C:\Forum\Stuff.net\hello@Rainmeter.exe
String1=C:\Forum\Stuff.net\Rainmeter.exe
String2=C:\Forum\Stuff.net\Rainmeter#2.exe
String3=https://forum.rainmeter.net/viewtopic.php?f=5&t=36150
String4=https://forum.something.rainmeter.net/viewtopic.php?f=5&t=36150
String5=https://viewtopic.php?f=5&t=36150
String6=scheme://username:password@host.domain:port/path?query#fragment
String7=scheme://username:password@other.host.domain:port/path?query#fragment
String8=scheme://username:password@168.246.2.15:port/pathpart1/pathpart2/pathpart3?query#fragment
String9=https://api.weather.com/v3/aggcommon/v3-wx-observations-current;v3-wx-forecast-daily-15day?format=json&geocode=26.38,49.50&units=m&apiKey=100

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255

---Measures---

[Iterator]
Measure=Calc
Formula=((Iterator+1)%#StringCount#)
UpdateDivider=-1

[FileOrHostName]
Measure=String
String="[#String[#StringIndex]]"
UpdateDivider=-1
RegExpSubstitute=1
Substitute="(?<!\/|\\)(?::|\/|\?)(?!\/|\\)[^:]*?$":"","^.*(?::.*?@|\\|\/)(?=.*\..*)":"",".*\.(?=.*?\..*?)":"","\..*?$":""
DynamicVariables=1

---Meters---

[Result]
Meter=STRING
FontFace=Consolas
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
FontSize=10
AntiAlias=1
MeasureName=FileOrHostName
Text="Text = [#String[#StringIndex]]#CRLF#Name = %1"
LeftMouseUpAction=[!SetVariable StringIndex ((#StringIndex#+1)%#StringCount#)][!UpdateMeasure FileOrHostName][!UpdateMeter Result][!Redraw]
DynamicVariables=1
Click on the skin to iterate through the test strings and see if the expected result is provided. As far as I could understand your question, it does - bar the relatively unlikely case of an URI having an IP as its host name, e.g. 168.246.2.15. While such a case can be handled, it doesn't suit well with the file name extraction from a local path in the same substitute, due to the presence of multiple period (.) characters. But then, that's just one case where it doesn't quite grab what it should, and a pretty fringe case too.
User avatar
StArL0rd84
Posts: 356
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: RegExpSubstitute Website name from URL

Post by StArL0rd84 »

Yincognito wrote: October 10th, 2020, 1:30 am RegExpSubstitute=1
Substitute="(?<!\/|\\)(?::|\/|\?)(?!\/|\\)[^:]*?$":"","^.*(?::.*?@|\\|\/)(?=.*\..*)":"",".*\.(?=.*?\..*?)":"","\..*?$":""
DynamicVariables=1
I really like your solution because it' so versatile.
It works really well, and to accommodate for folders and exe's too, i added your expression before mine, like this:
Substitute="(?<!\/|\\)(?::|\/|\?)(?!\/|\\)[^:]*?$":"","^.*(?::.*?@|\\|\/)(?=.*\..*)":"",".*\.(?=.*?\..*?)":"","\..*?$":"","^(.*)\\([^\\]*)$":"\2",".exe":""

But in my testing of different URL's, the following returns a 'co' not 'box' as you would expect.
https://www.box.co.uk/Cooler-Master-V850-SFX-850W-80-PLUS-Gold_3041886.html

Yincognito wrote: October 10th, 2020, 1:30 am in to iterate through the test strings and see if the expected result is provided. As far as I could understand your question, it does - bar the relatively unlikely case of an URI having an IP as its host name, e.g. 168.246.2.15. While such a case can be handled, it doesn't suit well with the file name extraction from a local path in the same substitute, due to the presence of multiple period (.) characters. But then, that's just one case where it doesn't quite grab what it should, and a pretty fringe case too.
And the fringe case of an IP as its host name i can live with, because I also gave the user of my skin the power to rename the string
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
StArL0rd84
Posts: 356
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: RegExpSubstitute Website name from URL

Post by StArL0rd84 »

balala wrote: October 9th, 2020, 8:00 pm Don't know. For instance from the https://www.rainmeter.net/ URL would you like to keep only rainmeter? Have I understood well?
If I am right, try something like this: Substitute="(.*)://(.*)\.(.*)\.(.*)":"\3"
Yeah, you are right, from the https://www.rainmeter.net/ URL i would like to keep only rainmeter, but it does not work with folders or exe's simultaneously.

Yincognito expression together with mine seems to work really well.
([mWorkTime] = 1 ? #Work# : ([mEnergyLoss:%] >= 70% ? #Chillmode# : </>))
User avatar
balala
Rainmeter Sage
Posts: 11575
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: RegExpSubstitute Website name from URL

Post by balala »

StArL0rd84 wrote: October 10th, 2020, 7:52 am but it does not work with folders or exe's simultaneously.

Yincognito expression together with mine seems to work really well.
Sorry my substitution hadn't even been designed to work with folders and exes, because I missread your original request, missing this detail.
Sorry again and feel free to use Yincognito's much better solution.
User avatar
Yincognito
Posts: 2760
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: RegExpSubstitute Website name from URL

Post by Yincognito »

StArL0rd84 wrote: October 10th, 2020, 7:38 am I really like your solution because it' so versatile.
It works really well, and to accommodate for folders and exe's too, i added your expression before mine, like this:
Substitute="(?<!\/|\\)(?::|\/|\?)(?!\/|\\)[^:]*?$":"","^.*(?::.*?@|\\|\/)(?=.*\..*)":"",".*\.(?=.*?\..*?)":"","\..*?$":"","^(.*)\\([^\\]*)$":"\2",".exe":""
But in my testing of different URL's, the following returns a 'co' not 'box' as you would expect.
https://www.box.co.uk/Cooler-Master-V850-SFX-850W-80-PLUS-Gold_3041886.html
And the fringe case of an IP as its host name i can live with, because I also gave the user of my skin the power to rename the string
Glad you like it - versatility is something I always look to have. First, I'm not sure your follow-up pattern is really needed for folders and exe-s, from what I can say my patterns already cover that (e.g. removing the extension, obviously from a 'name.ext' folder as well IF it's the last part in a path), but then, you know better what your requirements are, although an example of what the string would be to need additional regex patterns may be useful.

Yep, knew I forgot to mention something yesterday. It's true, it doesn't work for "co.uk"-like URIs, and I never attempted to make it work there, because first of all there is no way to "exclude" these type of URIs from stripping only the last .xxx part (in this case, .uk) without making or using a huge "database" of such URI types. The basic problem here is that you always want to get the penultimate (i.e. the last but one) period enclosed part from a string such as https://one.two.three.four/five/six.html (and there are PLENTY cases like this), yet this conflicts with having a string such as https://one.two.co.uk/five/six.html where co is taken simply because only uk is seen as a domain (or extension, if you like). You'll have this problem in other implmentations of such regex URI extraction, so it's not a specific case. I'm not sure what idea would work here, if any... :confused:
User avatar
Yincognito
Posts: 2760
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: RegExpSubstitute Website name from URL

Post by Yincognito »

StArL0rd84 wrote: October 10th, 2020, 7:38 amBut in my testing of different URL's, the following returns a 'co' not 'box' as you would expect: https://www.box.co.uk/Cooler-Master-V850-SFX-850W-80-PLUS-Gold_3041886.html
If you really want to cover this case, here's an (imperfect) handling of it:

Code: Select all

[Variables]
StringCount=12
StringIndex=0
String0=C:\Forum\Stuff.net\hello@Rainmeter.exe
String1=C:\Forum\Stuff.net\Rainmeter.exe
String2=C:\Forum\Stuff.net\Rainmeter#2.exe
String3=https://forum.rainmeter.net/viewtopic.php?f=5&t=36150
String4=https://forum.something.rainmeter.net/viewtopic.php?f=5&t=36150
String5=https://viewtopic.php?f=5&t=36150
String6=scheme://username:password@host.domain:port/path?query#fragment
String7=scheme://username:password@other.host.domain:port/path?query#fragment
String8=scheme://username:password@168.246.2.15:port/pathpart1/pathpart2/pathpart3?query#fragment
String9=https://api.weather.com/v3/aggcommon/v3-wx-observations-current;v3-wx-forecast-daily-15day?format=json&geocode=26.38,49.50&units=m&apiKey=100
String10=https://www.box.co.uk/Cooler-Master-V850-SFX-850W-80-PLUS-Gold_3041886.html
String11=C:\Forum\Stuff.net\Rainmeter.h.ax

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
BackgroundMode=2
SolidColor=47,47,47,255

---Measures---

[Iterator]
Measure=Calc
Formula=((Iterator+1)%#StringCount#)
UpdateDivider=-1

[FileOrHostName]
Measure=String
String="[#String[#StringIndex]]"
UpdateDivider=-1
RegExpSubstitute=1
Substitute="(?<!\/|\\)(?::|\/|\?)(?!\/|\\)[^:]*?$":"","^.*(?::.*?@|\\|\/)(?=.*\..*)":"",".*\.(?=.*?\..{,2}?\..{,2}?|.{3,}?\..{3,}?)":"","\.(?:.{,2}?\..{,2}?|.{3,}?)$":""
DynamicVariables=1

---Meters---

[Result]
Meter=STRING
FontFace=Consolas
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
FontSize=10
AntiAlias=1
MeasureName=FileOrHostName
Text="Text = [#String[#StringIndex]]#CRLF#Name = %1"
LeftMouseUpAction=[!SetVariable StringIndex ((#StringIndex#+1)%#StringCount#)][!UpdateMeasure FileOrHostName][!UpdateMeter Result][!Redraw]
DynamicVariables=1
Only the regex is slightly modified, but I included the whole code again because I added 2 test strings to better see the advantages and drawbacks of it. So your URL at String10 works well, BUT it yields what might be considered a wrong result at String11, which should actually be 'Rainmeter.h' since only '.ax' is the extension in local path cases (at least that's how Windows sees it). The pattern change was pretty simple, I basically included the 'something.xx.xx' (i.e. no more than 2 characters separated by period in domain / extension name) and left the other cases as covering the 3+ characters. This is again tricky and not quite covering all the actual instances on the Web, since it nowhere says that 'co.uk' is limited to 2 chars at most separated by period, they can be more than 2 characters in such situations actually (hence me mentioning a database of such occurences earlier).

Nevertheless, the pattern will do its job - it depends on you whether the drawbacks are acceptable or not, if you want to stick with the previous pattern(s) or like the latter one(s) more. ;-)