It is currently April 26th, 2024, 4:33 pm

IfMatch with special characters

Get help with creating, editing & fixing problems with skins
User avatar
Jaime Méndez
Posts: 98
Joined: August 31st, 2022, 10:00 pm

IfMatch with special characters

Post by Jaime Méndez »

Hello!

How can i use "RegExpSubstitute" to ask for exact match of a string containing only 3 characters like "C:\"

MeasureName=DiskName
IfMacht=C:\ | D:\ | E:\
IfMachtAction=[DoSomting]
User avatar
jsmorley
Developer
Posts: 22630
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: IfMatch with special characters

Post by jsmorley »

Jaime Méndez wrote: October 31st, 2022, 5:15 pm Hello!

How can i use "RegExpSubstitute" to ask for exact match of a string containing only 3 characters like "C:\"

MeasureName=DiskName
IfMacht=C:\ | D:\ | E:\
IfMachtAction=[DoSomting]
The "\" character is a reserved character in Regular Expression. It means "escape the next character and treat it as a literal rather than a reserved character." So to specify a literal "\" in a search parameter, you have to "escape" it, like this:

Code: Select all

IfMatch=C:\\|D:\\|E:\\
IfMatchAction=[!Log "Yessir, that's my baby. Nosir, I don't mean maybe"]
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5407
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: IfMatch with special characters

Post by eclectic-tech »

JSMorely beat me but I will leave my suggestions...
Jaime Méndez wrote: October 31st, 2022, 5:15 pm Hello!

How can i use "RegExpSubstitute" to ask for exact match of a string containing only 3 characters like "C:\"

MeasureName=DiskName
IfMacht=C:\ | D:\ | E:\
IfMachtAction=[DoSomting]
That's not RegExpSubstitute, you posted an IfMatch RegExpression test... but that is not important. :uhuh:

The string you are testing contains a reserved character \ (backslash) so you need to either "escape" that with another backslash or use :EscapeRegExp section variable.

This code manually escapes the backslash and disregards string case. (?i) will match any drive letter defined regardless of the string case.

Code: Select all

[MeasureDiskName]
Measure=String
String=[DiskName]
IfMatch=(?i)C:\\|D:\\|E:\\
IfMachtAction=[DoSomething]
This code automatically escapes reserved characters and disregards string case. (?i) will match any drive letter defined regardless of the string case.

Code: Select all

[MeasureDiskName]
Measure=String
String=[DiskName:EscapeRegExp]
IfMatch=(?i)C:\|D:\|E:\
IfMachtAction=[DoSomething]
DynamicVariables=1
Notice DynamicVariables=1 must be used with section variables.
User avatar
Jaime Méndez
Posts: 98
Joined: August 31st, 2022, 10:00 pm

Re: IfMatch with special characters

Post by Jaime Méndez »

eclectic-tech wrote: October 31st, 2022, 6:27 pm This code manually escapes the backslash and disregards string case. (?i) will match any drive letter defined regardless of the string case.

Code: Select all

[MeasureDiskName]
Measure=String
String=[DiskName]
IfMatch=(?i)C:\\|D:\\|E:\\
IfMachtAction=[DoSomething]
Thank you both! It was easer than i thought :thumbup: