std::regex vs std::wregex

What is the difference between std::regex and std::wregex?

In C++, std::regex and std::wregex are both used for working with regular expressions, but they serve different purposes depending on the type of strings you are working with.

std::regex

std::regex is used with std::string, which means it is designed to work with narrow-character strings (i.e., single-byte character strings).

This is the most common use case for regular expressions in C++ when dealing with standard ASCII or UTF-8 encoded text.

Example with std::regex:

#include <iostream>
#include <regex>

int main() {
  std::string text{"Hello world"};
  std::regex pattern{"world"};

  if (std::regex_search(text, pattern)) {
    std::cout << "Match found";
  } else {
    std::cout << "No match";
  }
}
Match found

std::wregex

std::wregex is used with std::wstring, which means it is designed to work with wide-character strings (i.e., multi-byte character strings).

This is useful when working with Unicode text that requires more than one byte per character, such as text in languages like Chinese, Japanese, or Korean.

Example with std::wregex:

#include <iostream>
#include <regex>

int main() {
  std::wstring text{L"Hello world"};
  std::wregex pattern{L"world"};

  if (std::regex_search(text, pattern)) {
    std::wcout << L"Match found";
  } else {
    std::wcout << L"No match";
  }
}
Match found

Key Differences

  • std::regex works with std::string (narrow characters).
  • std::wregex works with std::wstring (wide characters).
  • Use std::regex for standard ASCII or UTF-8 strings.
  • Use std::wregex for Unicode strings that require wide characters.

Understanding when to use std::regex versus std::wregex depends on the type of text you are processing.

For most applications involving standard English text or UTF-8 encoded text, std::regex is sufficient.

However, for applications that need to handle Unicode text with wide characters, std::wregex is the appropriate choice.

Regular Expressions

An introduction to regular expressions, and how to use them in C++ with std::regex, std::regex_match, and std::regex_search

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Debugging Complex Regex
How do I debug a complex regex pattern that isn't working as expected?
Real-World Regex Applications
What are some real-world applications of regex in software development?
Case-Sensitive Regex
How do I make a regex pattern case-sensitive or case-insensitive in C++?
Tools for Regex Testing
What tools can I use to test and visualize my regex patterns?
Regex Limitations in C++
Are there any limitations to using regex in C++ compared to other languages?
Combine Multiple Regex
How do I combine multiple regex patterns into one in C++?
Regex Replace in File
Can regex be used for replacing text in a file? How do I implement this in C++?
Regex Constants vs Traits
What are the differences between std::regex_constants and std::regex_traits in C++?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant