How to use regular expressions in Deephaven
Deephaven queries often filter data based on string values. This guide will show you how to filter your data based on string values in two ways:
- Using the Java String object and the methods they support.
- Using Deephaven's filters module, which allows for regex filtering on table data.
This guide does not provide a full overview of regular expressions (regex) but rather provides some examples of their use in tables.
Filter with built-in String methods
In this example, startsWith
is used in a where
filter to limit the table to only values where X
starts with A
.
from deephaven import new_table
from deephaven.column import string_col, int_col
source = new_table(
[
string_col("X", ["AA", "A x", "BaA", "5A", "a3B", "A"]),
int_col("Y", [3, 2, 1, 5, 6, 4]),
]
)
result = source.where(filters=["X.startsWith(`A`)"])
- source
- result
In this example, startsWith
is used with ||
to limit the table to only values where X
starts with A
or X
starts with a
.
result = source.where(filters=["X.startsWith(`A`) || X.startsWith(`a`)"])
- result
startsWith
is just one of the methods available. See the Javadoc for more built-in methods.
Filter with regex
If there is no pre-built method to search the string for your desired results, you can filter with regex by using the deephaven.filters
module.
In this example, a pattern filter limits the table to values where X
has three characters.
from deephaven.filters import PatternMode, pattern
threechar_filter = pattern(PatternMode.MATCHES, "X", "...")
result = source.where(threechar_filter)
- result
In this example, a pattern filter limits the table to values where X
contains a digit.
digit_filter = pattern(PatternMode.FIND, "X", "\d")
result = source.where(digit_filter)
- result