C++ Logo

std-proposals

Advanced search

Re: Escaping text for use in a regex

From: Giuseppe D'Angelo <giuseppe.dangelo_at_[hidden]>
Date: Wed, 11 Dec 2019 08:36:30 +0100
Il 10/12/19 14:54, Arthur O'Dwyer via Std-Proposals ha scritto:
> Btw, I should have mentioned Google RE2 among the state of the art. RE2
> basically treats regexes as strings (rather than forcing every regex to
> be "compiled" into a non-string object like std::regex). RE2 does
> provide `re2::QuoteMeta()` which takes a string and returns a string and
> does exactly what you asked for.
> https://github.com/google/re2/blob/master/re2/re2.h#L444-L450

Many other languages / libraries provide escaping functions (I've
personally written QRegularExpression::escape(), targeting PCRE).

Perl itself provides quotemeta(), Python provides re.escape(), PHP has
preg_quote(), Ruby has Regexp.escape(), and so on. Notably, JS' proposal
for adding a regexp escape functionality was rejected, apparently citing
concerns regarding composition of escaped fragments:

> https://github.com/benjamingr/RegExp.escape/issues/37

My 2 c,
-- 
Giuseppe D'Angelo | giuseppe.dangelo_at_[hidden] | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - The Qt, C++ and OpenGL Experts

Received on 2019-12-11 01:38:59