Skip to content

Filling in the error message variables

When we put the same names in error messages, and in a rule with the MustNotBeFound modifier, we fill the message with the data we got from the rule, and the thing to remember here is that no matter how many groups you create in one regular expression, we save the data from the entire regular expression

// text = "123123sd"
Rule::new(r"(?P<NUMBER_3>\d{3})(?P=NUMBER_3)\w+", MatchRequirement::MustBeFound)
1
2
3
4
5
// Output (log)
Some(Captures({
    0: Some("123123sds"),
    1: Some("123"),
})),
// text = "123123sd"
Rule(String.raw`(?P<NUMBER_3>\d{3})(?P=NUMBER_3)\w+`, MatchRequirement.MustBeFound)
1
2
3
4
5
// Output (log)
Some(Captures({
    0: Some("123123sds"),
    1: Some("123"),
})),
# text = "123123sd"
Rule(r"(?P<NUMBER_3>\d{3})(?P=NUMBER_3)\w+", MatchRequirement.MustBeFound)
1
2
3
4
5
// Output (log)
Some(Captures({
    0: Some("123123sds"),
    1: Some("123"),
})),

we skip the match from the NUMBER_3 (1) subgroup, we keep the overall result from the expression (0).


When you specify the message in Cartridge

1
2
3
4
5
let cartrdige_0 = Cartridge::new(
        0,
        "error message with value {INFO}",
        [...],
    );
1
2
3
4
5
let cartrdige_0 = new Cartridge(
        0,
        "error message with value {INFO}",
        [...],
    );
1
2
3
4
5
cartrdige_0 = Cartridge(
        0,
        "error message with value {INFO}",
        [...],
    )

you can specify the same name as the name of the capture group in a regular expression

Rule::new(r"(?<INFO>\d+)", MatchRequirement::MustNotBeFound)
Rule(String.raw`(?<INFO>\d+)`, MatchRequirement.MustNotBeFound)
 Rule(r"(?<INFO>\d+)", MatchRequirement.MustNotBeFound)

If you want to output only one variable, you can use the reserved name without assigning a group name to the regular mode main_capture.

1
2
3
4
5
6
7
let cartrdige_0 = Cartridge::new(
        0,
        "error message with value {main_capture}",
        [
            Rule::new(r"(?<main_capture>\d+)", MatchRequirement::MustNotBeFound),
        ],
    );
1
2
3
4
5
6
7
let cartrdige_0 = new Cartridge(
        0,
        "error message with value {main_capture}",
        [
            Rule(String.raw`(?<main_capture>\d+)`, MatchRequirement.MustNotBeFound),
        ],
    );
1
2
3
4
5
6
7
cartrdige_0 = Cartridge(
        0,
        "error message with value {main_capture}",
        [
            Rule(r"(?<main_capture>\d+)", MatchRequirement.MustNotBeFound),
        ],
    )