regex: ] in a character class

kdevel kdevel at vogtner.de
Sat Dec 12 12:03:49 UTC 2020


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


More information about the Digitalmars-d-learn mailing list