Regex backslash delimiter

I'm trying to figure out how to use a backslash as part of a string delimiter using regex.

My code is suppose to split a line when it comes across \aBCDEF but I can't get it to split with the backslash included.

if I set line 20 to split_regex( fields, buffer, boost::regex( "aBCDEF " ) )

it splits just fine. I don't know if it's possible to use a backslash as part of a delimiter with regex but somebody here may know.

I've tried using double backslashes and it didn't work either.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <iostream>
#include <fstream>
#include <string>
//#include <conio.h> //getch()

void parse_file (std::ifstream& file_in){
    std::string buffer;//buffer to store lines of raw data

    while (!file_in.eof()){



        //read the raw data into the buffer
        getline(file_in, buffer);

        std::vector <std::string> fields;

        split_regex( fields, buffer, boost::regex( "\aBCDEF " ) );

        //print out the fields
        for (size_t n = 0; n < fields.size(); n++){
            std::cout << "\"" << fields[ n ] << "\"\n";
        }

        //std::cout << "\n\nPress any key to loop again\n\n";
        //getch(); //commented out for now
        file_in.close();
        break;//testing

    }//while loop bracket
}
Last edited on
This works here.

split_regex( fields, buffer, boost::regex( "\\\\aBCDEF " ) );

I guess you need to include three backslashs then the special characters

so this now functions the way I wanted it too.

It is the three backslashs to keep it from being escaped and then the backslash for the character at least that's how I think it was working

I can understand three backslashes, (one for c++, one for regex, and one for the character) but the fourth I'm uncertain why?

Maybe it's escaped for regex then boost then c++ then the character?

I'm just making this up and I have no idea how it actually works but if anybody knows please set me straight. Either way, I now know I need three backslashes then the character so on to the next problem ;p
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    std::cout << "\\\\aBCDEF"  << '\n' ; // C-style literal for regex \\aBCDEF
    // each backslash needs to be escaped, we need two of them for the regex
    // two \\ for the first escaped backslash, two \\ for the second escaped backslash
    // adding up to four. "\\\\" is the C-style literal for two backslashes in succession

    // in C++, we should prefer a raw string literal for constructs of this kind
    // http://www.stroustrup.com/C++11FAQ.html#raw-strings
    std::cout << R"(\\aBCDEF)" << '\n' ; // C++ raw string literal for regex \\aBCDEF
}

http://coliru.stacked-crooked.com/a/598ae16406fb1032
interesting.

I will have to read more about this "raw" data format.

Thanks for the explanation.
Topic archived. No new replies allowed.