regex_search icase

Sample code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <regex>
#include <string>

int main()
{
    std::wregex regex;
    std::wstring line;

    // This compiles
    std::regex_search(line, regex, std::regex_constants::match_default);

    // This doesn't compile
    std::regex_search(line, regex, std::regex_constants::icase);
}


Can somebody please enlighten me why the second case doesn't work?
Last edited on
They have different types. Compare match_flag_type with syntax_option_type:
https://en.cppreference.com/w/cpp/regex/match_flag_type
https://en.cppreference.com/w/cpp/regex/syntax_option_type

Specify the syntax option when constructing the regex, specify match flags when matching the regex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <regex>
#include <string>
#include <iostream>

int main()
{
    std::wregex regex { L"abc", std::regex_constants::icase };
    std::wstring line { L"aBc" };

    if (std::regex_search(line, regex)) 
      std::cout << "matched\n"; 
    else 
      std::cout << "did not match\n";
}
Last edited on
Thanks, I see, different types hah :)

I was really hoping to avoid constructing a regex beforehand because I have one and same regex object which I reuse by reassigning a new pattern each time it is used.

Something like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <regex>
#include <string>

int main()
{
    std::regex regex;
    std::string line;

    regex = "something";
    std::regex_search(line, regex);

    regex = "something";
    std::regex_search(line, regex);

    regex = "something";
    std::regex_search(line, regex);

    // etc..
    // However this time it must be case insensitive:
    
    // how to make is case insensitive here without constructing a new one?
    regex = std::regex("something", std::regex_constants::icase);
    std::regex_search(line, regex);
}


This last case, is there a more efficient alternative to avoid a copy ctor?
Is it possible to make existing regex object case insensitive? ex. to manipulate it.
Last edited on
This last case, is there a more efficient alternative to avoid a copy ctor? Is it possible to make existing regex object case insensitive? ex. to manipulate it.

I don't think it's possible to adjust the syntax options of an existing regex.
Last edited on
Thanks buddy, appreciate your help!
1
2
3
// how to make is case insensitive here without constructing a new one?
// https://en.cppreference.com/w/cpp/regex/basic_regex/assign
regex.assign( "something", std::regex_constants::icase );


Note: this may not be much more efficient than constructing a new regex.
I said "copy ctor" while in fact I should have said "move ctor".

regex.assign uses copy semantics from what I see, while in my sample in previous post move ctor is used since temporary object is used.

So I may agree with you that it's likely not more efficient.
The expensive part of constructing a typical regex is probably the process of compiling it into a finite automaton that can be interpreted, rather than any memory-to-memory copy or move construction/assignment.
Last edited on
Topic archived. No new replies allowed.