85951

How can I match the \\0 character in a regex in C++?

I need to match the text '\0' with the same regex that I would match 'a' or 'b'. (a regex for a character constant in C++). I've tried a bunch of different regexes, but haven't gotten a successful one yet. My latest attempt:

^['].|\\0[']

Most of the other things I've tried have given seg faults, so this is really the closest I've gotten.

Answer1:

This works pretty nicely with what I've tested ('a','b','\0').

If you don't have std::regex or boost::regex I guess what you can get out of it is the fact that the regex I used is ('.'|'\\0').

#include <boost/regex.hpp> #include <string> #include <iostream> #include <vector> int main() { std::vector<std::string> strings; strings.push_back(R"('a')"); strings.push_back(R"('b')"); strings.push_back(R"('\0')"); boost::regex rgx(R"(('.'|'\\0'))"); boost::smatch match; for(auto& i : strings) { if(boost::regex_match(i,match, rgx)) { boost::ssub_match submatch = match[1]; std::cout << submatch.str() << '\n'; } } }

Example

Answer2:

There's nothing magic about '\0'; it's just a character, like any other character, and there's nothing (almost) special you have to do to use it in a regular expression. The only problem you might run into is if you use it in the middle of a character literal that you pass to a function that treats it as the end of a string. To avoid that, force it into a std::string:

const char s[] = "a\0b"; std::string not_my_str(s); // not_my_str holds "a" std::string str(s, 3); // str holds "a\0b"

Once you've constructed the string object, the embedded '\0' gets no special treatment. Except, of course, if you copy the contents with a function that treats it specially.

Answer3:

The regex that works (in this instance, using the C header ) is:

^('(.|([\\]0))')

Thanks to @WhozCraig for the help!

Recommend

  • Array of contiguous new objects
  • why is the sort() not working?
  • How to compare results between C++ and MATLAB?
  • CGAL: How to efficiently calculate the area of facets of a polyhedron?
  • Why can't the compiler deduce template parameter from return type?
  • Interrupting boost thread
  • SOCKS in C/C++ or another language?
  • C++ Boost ptr_map serialization error
  • C++ and JS RegEx equivalent of \\p{L}
  • How can I emulate a recursive type definition in C++?
  • Result showing lining on image
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • Building Qt project for C++11 standard
  • C++ friend class std::vector
  • netsh acl setting (need alternative method - registry settings?)
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • how to set variables in a php include file?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Recording logins for password protected directories
  • What is the “return” in scheme?
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Buffer size for converting unsigned long to string
  • Hits per day in Google Big Query
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • How to get Windows thread pool to call class member function?
  • apache spark aggregate function using min value
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Sorting a 2D array using the second column C++
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal