Wildcard on String Matching

I have a opc string tag that may contain “BC0”, “BC1”, “BC2”, “TQ0”, “TQ1”, “TQ2”… etc. depending on the instructions that was selected by the user.

I want to script something on Ignition when the tag says “BC”, TQ, or UC.

an example of my script would be: if({tag} = “BC(wildcard?)” , 1,0)

I am just trying to minimize my scripting and avoiding writing the script like:
if({tag} = “BC0” , 1,0 &&
if({tag} = “BC1” , 1,0) &&
if({tag} = “BC2” , 1,0)))

Thanks in advance!

You can use something like this.

if({tag} like 'BC%', 1, 0)
3 Likes

That worked! Thanks! I did not know that I can use “like”

Yeah, the like operator is very situational but very useful. It's also worth noting - you don't actually need an if statement if your only return is 1/0 (ie, true/false) - you can just use chain boolean operators directly:
{tag} like 'BC%' || {tag} like 'TQ%' will return a true value if either condition is met.

This appears to be expression language rather than actual python scripting, right?

What about doing something like

tag = "BlahBlahBC1BlahBlah"
a=['BC0','BC1','BC2','TQ0','TQ1','TQ2']
Found=1 if any(x in tag for x in a) else 0
print Found

1 Like

Yes - the OP used 'scripting' but has an example that looks more like expression syntax, so I rolled with it.

This certainly works, although it's still an unnecessary if; the following has the exact same result:

tag = “BlahBlahBC1BlahBlah”
a=[‘BC0’,‘BC1’,‘BC2’,‘TQ0’,‘TQ1’,‘TQ2’]
print any(x in tag for x in a)
2 Likes