# Logical and arithmetic expressions

Logical and arithmetic expressions can be used in `SHOW` clauses and `WHERE` clauses.

The following topics are covered in this section:

## Truth

In `WHERE` clauses, the following values are considered False:

 Boolean False Number zero Empty string Empty list None (for example, missing attribute)

All other values are considered True. This allows simple `WHERE` clauses that choose only useful values. For example, to find all Software Instance nodes with a populated version:

```SEARCH SoftwareInstance WHERE version SHOW type, version
```

## Expressions

The following logical and arithmetic expressions are supported:

 Equality `a = b` Inequality `a <> b` Comparison `a > b` `a >= b` `a < b` `a <= b` Arithmetic `a + b` `a - b` `a * b` `a / b` Subwords `a HAS SUBWORD b` Case-insensitive subword or phrase test. a is split into a word list using non-alphanumeric characters to identify the word boundaries.  For a single word search, the condition is true if `b` is in the list of split words.For a multi-word search:To find a number of words in the text, disregarding the word order, enclose the words with quotation marks, for example: `os_type has subword "Red Hat Linux"` This condition is true if all the words "Red", "Hat", and "Linux" are found as subwords of the full text, not limiting results to the exact phrase match. For example, if os_type is "Red Hat Enterprise Linux", the search in the previous example returns true.To find an exact phrase in the text, taking into account the word order, enclose the phrase in square brackets, like in the following example: `os_type has subword ["Red Hat Linux"]` This condition is true if the exact phrase is found as a subphrase of the full text, matching the word order. For example, if os_type is "Red Hat Enterprise Linux", the condition in the previous example returns false.When used in a `WHERE` clause, `HAS SUBWORD` uses the full text indexes maintained by the datastore. It is one of the most efficient ways to find nodes. Substrings `a HAS SUBSTRING b` Case-insensitive substring test. Where possible `HAS SUBSTRING` uses the full text indexes maintained in the data store, but it is always less efficient than `HAS SUBWORD`. Regular expression match `a MATCHES b` `a LIKE b` The condition is True if `a` matches regular expression `b`. `LIKE` is a deprecated synonym of `MATCHES`. `MATCHES` cannot often use the datastore indexes, and is therefore by far the slowest mechanism for finding nodes. If at all possible queries should use `HAS SUBWORD` instead of `MATCHES`. Containment `a IN b` `a NOT IN b` The condition is True if `a` is/is not in `b`, where `b` is a list. Containment `a IN [b, c, d...]` `a NOT IN [b, c, d...]` The condition is True if `a` is/is not in the specified list. Definition `a IS DEFINED` `a IS NOT DEFINED` The condition is True if the node has an attribute `a` (with any value). Inverse `NOT a` True if `a` is considered False; False if it is considered True. And `a AND b` Considered True if both `a` and `b` are considered True. If `a` is considered True, the value of the expression is `b`; if `a` is considered False, the value of the expression is `a`. Or `a OR b` Considered True if either `a` is considered True or `b` is considered True. If `a` is considered True, the value of the expression is `a`; otherwise the value of the expression is `b`.

## Precedence

Operator precedence works as you would expect. Parentheses `(` and `)` are used to explicitly group operations. Otherwise, in arithmetic expressions, `*` and `/` take precedence over `+` and `-`. In logical expressions, `OR` has lowest precedence, followed by `AND`, followed by `NOT`, followed by all the other operators. i.e. this expression

```NOT a > b OR c HAS SUBWORD d AND e = 10
```

is equivalent to

```(((NOT (a > b)) OR ((c HAS SUBWORD d) AND (e = 10)))
```

## Logical expressions in SHOW clauses

Logical expressions can be used in `SHOW` clauses. To do so, they must be enclosed in parentheses, e.g.

```SEARCH Host SHOW name, (ram > 1024)
```

The behavior of `AND` and `OR` in returning one of their input parameters can be useful in handling missing attributes, to avoid output of "None" when an attribute is not available, for example:

```SEARCH Host SHOW name, (domain OR "Unknown domain")
```
```SEARCH Host SHOW name, (virtual AND "Host identified as virtual"
OR "Host not identified as virtual")```

## Logical expression name binding

Logical expressions can be bound to names. For example:

```SEARCH Host
WITH (virtual AND "yes" OR "no") AS is_virtual
SHOW name, @is_virtual
```

## Using a regular expression

The MATCHES condition enables you to search by matching against a regular expression (or regex), a pattern that matches various text strings. For example, `A[0-9]+` matches any string that consists of the letter A followed by one or more digits.

Regular expressions have a defined syntax that enables you to define complex matching patterns. BMC Discovery uses the Python implementation; for full syntax and details of use, consult the Python documentation. For more information, see https://docs.python.org/2/library/re.html.

The following table lists a few of the matching characters that you can use when constructing regular expressions. An ordinary character, or a sequence of characters, matches that character or string.

Character

Details

.

A dot matches any single character.

^

A caret matches characters at the start of the string.

\$

A dollar sign matches characters at the end of the string.

*

An asterisk matches 0 or more repetitions of the preceding regex. For example, ab* matches "a," "ab," or "a" followed by any number of "b"s.

+

A plus sign matches one or more repetitions of the preceding regex. For example, ab+ matches "a" followed by any nonzero number of "b"s; it does not match just "a."

?

A question mark matches 0 or 1 repetitions of the preceding regex. For example, ab? matches either "a" or "ab."

[ ]

Square brackets indicate a set of characters that can be matched. For example, "asdf" matches any of the characters "a," "s," "d," "f."

|

The vertical bar separates regular expressions, any or which can be matched. For example, A|B matches either A or B.

\

The backward slash followed by any special character matches the special character itself.

## Related topics

Using the Query Language

Submitting... Thank you