Join us today!

Notifications
Clear all

[Solved] Wildcard comparison of strings

5 Posts
2 Users
2 Reactions
751 Views
Posts: 6
Topic starter
(@chrisbeardy)
Active Member
Joined: 2 years ago

Hello, has anyone written or know of any functions in ST that can compare two strings, including wildcards where _ (underscore) is a substitute for one character and * can be a substitute for 1 or more chars. I want the following behaviour:

 

CompareStrings('hello', 'hello') = TRUE

CompareStrings('h_llo', 'hello') = TRUE

CompareStrings('h__lo', 'hello') = TRUE

CompareStrings('h_l_o', 'hello') = TRUE

CompareStrings('_ello', 'hello') = TRUE

CompareStrings('h*', 'hello') = TRUE

CompareStrings('h*o', 'hello') = TRUE

CompareStrings('h*l_', 'hello') = TRUE

CompareStrings('h*lo', 'help') = FALSE

CompareStrings('h_llo', 'hell') = FALSE

CompareStrings('h_ll', 'hello') = FALSE

CompareStrings('he_o', 'help') = FALSE

CompareStrings('h*o', 'help') = FALSE

 

I am struggling with the one or more characters bit.

Reply
4 Replies
Posts: 7
(@theshamot)
Active Member
Joined: 2 years ago

Hey pal,

you can try out my - sketchy - very basic '^$.*+' - C copypasta/conversion - poor performance - Regex matcher i made for TwinCAT.

Usage should be something like

fbRegex.match(pattern := 'he.+o', text := 'hellllo').found = TRUE

https://github.com/theshamot/tc-syc/blob/master/SYC/SYC/Tools/Regex.TcPOU

 

 

Reply
3 Replies
(@chrisbeardy)
Joined: 2 years ago

Active Member
Posts: 6

@theshamot this looks great, top work. It falls over in a couple of places e.g. (hello vs *llo and hello vs hell) but is otherwise sound! Thank you.

Reply
(@theshamot)
Joined: 2 years ago

Active Member
Posts: 7

@chrisbeardy thank you, I will maybe try to optimize and fix it sometime. Also "*llo" is invalid syntax... "*" means there is zero or more of character that is before "*" - So "He*llo" will match strings like this: "Hllo", "Hello", "Heello", "Heeeeello".... You can use "." (dot) to match any character - "H.llo" will match strings like "Hello", "HHllo", "H1llo", "H llo"....

visit this page to learn regex https://regex101.com . Rules are explained in right bottom of webpage.

Reply
(@chrisbeardy)
Joined: 2 years ago

Active Member
Posts: 6

@theshamot yes of course, getting my regex and linux wildcards mixed up!, in which case ".*lo" works against "hello"!

Reply
Share: