Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does std::regex support "(?i)" for case insensitivity?

Tags:

c++

regex

I am using Visual Studio 2010. This...

std::regex pattern("(?i).*a.*");

...throws this...

std::tr1::regex_error - regular expression error

...and I can't find anything that says if std::regex supports the (?i) syntax for case insensitivity or not.

Can anyone confirm/deny that (?i) is not supported as a prefix for case insensitivity by std::regex?

like image 395
John Fitzpatrick Avatar asked Jul 14 '14 19:07

John Fitzpatrick


People also ask

How do you make a case insensitive in regex?

Case-Sensitive Match To disable case-sensitive matching for regexp , use the 'ignorecase' option.

Are regex expressions case-sensitive?

By default, the comparison of an input string with any literal characters in a regular expression pattern is case-sensitive, white space in a regular expression pattern is interpreted as literal white-space characters, and capturing groups in a regular expression are named implicitly as well as explicitly.

Does case matter in regex?

Discussion. Currently, REGEXP is not case sensitive, either.

Is regex case-sensitive C#?

The Regex type in the C# language by default is case-sensitive. But RegexOptions. IgnoreCase, an enum value, relaxes this. Example.


2 Answers

The standard only requires conformance to the POSIX regular expression syntax (which does not include Perl extensions like this one) and conformance to the ECMAScript regular expression specification (with minor exceptions, per ISO 14882-2011§28.13), which is described in ECMA-262, §15.10.2. ECMAScript's regular expression grammar does not include the use of modifiers in the form of the (?) syntax, so, by extension, neither does C++11/14, nor do most implementations of TR1.

That does not preclude your standard library from implementing more PCRE extensions, but the standard does not require it, so it's simply not guaranteed.

So, no, it's not supported, per se.

You can, however declare your regular expression as follows:

std::regex pattern(".*a.*", std::regex_constants::icase);

This will declare your pattern to be case-insensitive.

like image 153
greyfade Avatar answered Oct 05 '22 11:10

greyfade


boost::regex supports Perl syntax, which has (?i).

like image 39
ulatekh Avatar answered Oct 05 '22 09:10

ulatekh