Donβt let the name of this function fool you. The fnmatch
function is a simple, yet, powerful tool to match any text (not just file names) against a pattern. It is an very easy and powerful way to match text against a pattern, without having to learn regular expressions.
Template function properties | |
---|---|
Function | Match text or a lists of texts against a pattern |
Function name | fnmatch |
Returns | If the given text matches the give pattern |
Return type | boolean |
Can be used as a filter | Yes |
Can be used as a test | Yes |
Spook's influence | Newly added template function |
Developer tools | Try this in the template developer tools |
Signature |
---|
|
Function parameters | |||
---|---|---|---|
Attribute | Type | Required | Default / Example |
value | string, list of strings | Yes | Spook |
pattern | string | Yes | sp*k |
case_sensitive | boolean | No | False |
By default, this function is not case-sensitive. This means that spook
and Spook
are considered the same. If you want to make the function case-sensitive, set the case_sensitive
parameter to True
.
PatternsΒΆ
The UNIX shell-style pattern is a relatively simple, easy to learn pattern matching technique, which is not the same as regular expressions.
The following characters are special characters in a pattern:
Character | Meaning |
---|---|
* | Matches anything (one, zero or multiple characters) |
? | Matches any single character |
[seq] | Matches any character in sequence |
[!seq] | Matches any character not in sequence |
The sequence of characters in [seq]
can be a range, for example: [a-z]
or [0-9]
, or a list of characters, for example: [abc]
or [123]
.
If you want to match any of these special chacters (*
, ?
, [
or ]
) literally, you must wrap them in square brackets ([]
), for example: [?]
.
This is all technical details you need to know about the pattern matching, but letβs look at some examples.
- Pattern
spook
matchesspook
. - Pattern
sp*k
matchesspook
,spooook
, but alsospacewalk
,speak
, andspank
π - Pattern
[hb]ook
matcheshook
andbook
. - Pattern
[!hb]ook
matchescook
,look
(and many more), but nothook
orbook
. - Pattern
t?k
matchestik
,tok
, and character in the second position, but nottook
.
Hopefully, this gives you a good idea of how to use the pattern matching. In practice, you probably will end up using the *
and ?
characters the most.
ExamplesΒΆ
Using fnmatch as a functionΒΆ
1 2
{{ fnmatch("Spook", "sp*k") }} {{ fnmatch("Spook", "spa?k") }}
Returns:
True
False
Using fnmatch as a filterΒΆ
1 2
{{ "Spook" | fnmatch("sp*k") }} {{ "Spook" | fnmatch("spa?k") }}
Returns:
True
False
Using fnmatch as a testΒΆ
1 2 3
{% if "Spook" is fnmatch("sp*k") %} Spook matches! {% endif %}
Using fnmatch to match a lists of textsΒΆ
You can also pass a list of texts to the fnmatch
function. The function will return True
if all of the texts in the list matches the pattern. If one of the texts does not match, the function will return False
.
1 2
{{ fnmatch(["Spook", "Spook2"], "Sp*") }} {{ ["Spook", "Ghost"] | fnmatch("Sp*") }}
Returns:
True
False
Case-sensitive matchingΒΆ
By default, the fnmatch
function is not case-sensitive. This means that spook
and Spook
are considered the same. If you want to make the function case-sensitive, set the case_sensitive
parameter to True
.
1 2
{{ fnmatch("Spook", "sp*k", case_sensitive=True) }} {{ fnmatch("Spook", "sp*k", case_sensitive=False) }}
Returns:
False
True
Features requests, ideas, and supportΒΆ
If you have an idea on how to further enhance the Home Assistant template engine, for example, by adding a new template function; feel free to let us know in our discussion forums.
Are you stuck using this new feature? Or maybe youβve run into a bug? Please check the Support page on where to go for help.