> ## Documentation Index
> Fetch the complete documentation index at: https://docs.teal.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Crafting rules

> How to craft categorization rules

A rule consists of three components:

* **Expression**: A pattern matching expression using the `match(pattern, field)` function that is evaluated against each transaction to determine whether it satisfies the criteria of the rule.
* **Priority**: If multiple rules match a transaction, the rule with the highest priority will be implemented first.
* **Destination Ledger**: This is the name of the ledger into which the transaction will be categorized if the rule conditions are met. If the specified ledger name doesn’t exist in an instance’s chart of accounts, the system will disregard the rule.

#### Expressions

Expressions are written using the `match(pattern, field)` function, which checks if a `field` of the Transaction object matches a `pattern`.

#### The Transaction object

The entire `Transaction` object is accessed via the symbol `t` and references specific fields using `.`, for example, `t.description` or `t.amount`.

```json theme={null}
{
    {
      "amount": 100,
      "datetime": "2022-01-01T00:00:00Z",
      "description": "Shell Gas Bar 4124",
      "id": "t_9237232",
      "metadata": {
        // ...
      },
      "posted_status": "posted",
      "review_status": "reviewed"
    }
}
```

If `metadata` was added to the `Transaction`, you can reference it through `t.metadata`. See [submitting transactions](/guides/transactions/submitting) for more information on adding metadata to your transactions.

#### Using `match()`

Use `match(pattern, field)` to check if a `field` of the `Transaction` object matches a `pattern`. The `pattern` must be a `string` and supports regular expression syntax for writing complex rules.

You can learn more about regular expressions and their syntax [here](https://developers.google.com/edu/python/regular-expressions).

#### Examples

##### **Payments to a specific vendor**

A rule that checks if the Transaction's description contains "Slack", preceded by any number of any characters:

```python theme={null}
match(".*Slack", t.description)
```

will match

```json theme={null}
{
    "t": {
        "description": "Abc 7890Slack xYz"
        // ...
    }
}
```

##### **Case-insensitive matching**

Use `(?i)` in the pattern to make the match case-insensitive:

```python theme={null}
match(".*(?i)chevron", t.description)
```

will match descriptions like "CHEVRON", "chevron", or "Chevron Gas Station".

##### **Transactions with certain metadata**

A rule that looks for transactions with a `counterparty` attribute in the metadata matching "Sophie's Contracting LLC":

```python theme={null}
match("Sophie's Contracting LLC", t.metadata.counterparty)
```

will match

```json theme={null}
{
    "t": {
        "metadata": {
            "counterparty": "Sophie's Contracting LLC"
        }
        // ...
    }
}
```

***

## Relevant resources

* [Submitting transactions](/guides/transactions/submitting)
* [Reviewing transactions](/guides/transactions/reviewing_transactions)
* [Chart of accounts template](/guides/platform/chart_of_accounts_config)
