relationalai.std.re.escape()
escape(regex: str|Producer) -> Expression
Escapes special characters in a regular expression string.
Use this to match an arbitrary string that may contain regular expression metacharacters.
If regex
is a Producer
, then escape()
filters out non-string values from the producer.
Must be used in a rule or query context.
Parameters
Section titled “Parameters”Name | Type | Description |
---|---|---|
regex | Producer or Python str | The string to escape. |
Returns
Section titled “Returns”An Expression
object.
Example
Section titled “Example”Use the escape()
function to escape regex metacharacters in a string:
import relationalai as raifrom relationalai.std import re
# =====# SETUP# =====
model = rai.Model("MyModel")SearchQuery = model.Type("SearchQuery")
with model.rule(): SearchQuery.add(id=1).set(raw=".ai") SearchQuery.add(id=2).set(raw="snow") SearchQuery.add(id=3).set(raw=123) # Not a string
# =======# EXAMPLE# =======
# Set an escaped property on each SearchQuery object that escapes regex# metacharacters in the raw property.with model.rule(): search_query = SearchQuery() # Since escape() filters out non-string values, the following does not set # the escaped property on the SearchQuery object with id=3. search_query.set(escaped=re.escape(search_query.raw))
with model.query() as select: search_query = SearchQuery() response = select(search_query.raw, search_query.escaped)
print(response.results)# raw escaped# 0 123 NaN# 1 .ai \.ai# 2 snow snow