Tinderbox Forum

Using logical operators with String.contains and String.icontains?

While there is probably some regex that can be used to do it in one shot, am I right in thinking that to search for “foo” and “bar” in (say) $Text you have to write the following:

$Text.icontains("foo") & $Text.icontains("bar")

whereas

$Text.icontains("foo" & "bar") won’t parse correctly?

Or is there some trick to combining them in the one argument?

Thanks in advance.

It’s simpler to have two .contains()

Just checking, is that meant to be “two .contains()”? Pretty sure it is, and if so thanks for the tip.

1 Like

In short, yes. Although people most often use .contains() and .icontains() with literal strings for matching, nonetheless, the operator actually uses a quote-enclosed regex pattern, i.e. .contains("pattern"). Of course, a lieral string is a valid pattern.

You can use the pipe | operator within a regex to simulate an OR join, but an AND is harder. As @eastgate notes, just put each pattern in a separate operator.

If using multiple .contains() with an & join, if there is a likely disparity in the rate of occurrence of the two patterns. Put the text for the less common one first, as the second test only matches the strings passing the first text, i.e. lessening the amount of overall checking. In most TBXs you’ll never notice the difference but it is a good habit to acquire for when/if you move to using large documents where it will help overall snippiness of operation.

1 Like

From the tiny bit I know about regex, I remember that that’s the case. Thanks for the other tips.

1 Like