Join us today!
[Solved] Wildcard comparison of strings
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.
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
@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.
@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.
@theshamot yes of course, getting my regex and linux wildcards mixed up!, in which case ".*lo" works against "hello"!
- 17 Forums
- 267 Topics
- 942 Posts
- 0 Online
- 722 Members