digitalmars.D.learn - regex: ] in a character class
- kdevel (17/17) Dec 12 2020 In some situations a ] must be escaped as in
- Tobias Pankrath (4/23) Dec 12 2020 As I understand it, the statement is indeed true and a regex
- Tobias Pankrath (3/5) Dec 12 2020 This [1] is how I would word it.
In some situations a ] must be escaped as in
auto re = regex(`^[a\]]$`); // match a and ] only
Unfortunately dmd/phobos does not warn if you forget the
backslash:
auto re = regex(`^[a]]$`); // match a]
This leads me to the documentation [1] which says
\c where c is one of [|*+?() Matches the character c itself.
] must be added to this list since \] obviously matches ].
Additionally
the statement
any character except [{|*+?()^$ Matches the character
itself.
is not true since ] does not match itself when ] denotes the end
of
a character class. I don't have a suggestion for better wording
yet.
[1] https://dlang.org/phobos/std_regex.html
Dec 12 2020
On Saturday, 12 December 2020 at 12:03:49 UTC, kdevel wrote:In some situations a ] must be escaped as in auto re = regex(`^[a\]]$`); // match a and ] only Unfortunately dmd/phobos does not warn if you forget the backslash: auto re = regex(`^[a]]$`); // match a] This leads me to the documentation [1] which says \c where c is one of [|*+?() Matches the character c itself. ] must be added to this list since \] obviously matches ]. Additionally the statement any character except [{|*+?()^$ Matches the character itself. is not true since ] does not match itself when ] denotes the end of a character class. I don't have a suggestion for better wording yet. [1] https://dlang.org/phobos/std_regex.htmlAs I understand it, the statement is indeed true and a regex `]]]` would match and only match the string `]]]`. What should be added somewhere isInside character classes the character ']' has to be written as '\]'.
Dec 12 2020
On Saturday, 12 December 2020 at 12:03:49 UTC, kdevel wrote:I don't have a suggestion for better wording yet. [1] https://dlang.org/phobos/std_regex.htmlThis [1] is how I would word it. [1] https://github.com/dlang/phobos/pull/7724
Dec 12 2020









Tobias Pankrath <tobias+dlang pankrath.net> 