C++ Logo


Advanced search

Subject: [std-proposals] Escaping text for use in a regex
From: Stephan Reiter (stephan.reiter_at_[hidden])
Date: 2019-12-06 08:31:49


I encountered a situation where I needed to incorporate user input
into a regular expression.
To safely do this, I needed to escape regex-special characters in the
user input.

For inclusion into the standard library, I'd like to propose the
following function (plus variations as needed, e.g. also for

std::string regex_escape(std::string_view text,
std::regex_constants::syntax_option_type f =

The function returns a regex pattern that matches against the given
input string. It supports different regex styles, e.g. ECMAScript.

Based on the function output, I could build dependent regex patterns, e.g.
std::string pattern = regex_escape(user_selected_dir) + "/*\\.log";

The presence of this function in the standard library will make it
easier to use regex and will also contribute to raising awareness
about the need to escape user inputs.

What do you think?

Kind regards,

STD-PROPOSALS list run by std-proposals-owner@lists.isocpp.org

Standard Proposals Archives on Google Groups