Regular Expressions in C++

Hello All, I have recently been experimenting with Regular Expressions in C++ (Regex). Fortunately, I have worked with Regex before, but to my suprise, none of my expressions that I am familiar with as far as syntax work in C++. I am trying to isolate anything between two delimeters, but without the delimeters in the output. I need to loop through each result accordingly.

For example:

<Beginning>hi<End>

I want the code to return hi. I have tried the following code/Regular Expressions:

1
2
3
4
5
6
7
8
int main()
{
	std::string str("This is just a simple sentence <Bank>hi<Bank> :start hi :end :start bob :end");
	std::regex r(":start (.*?) :end"); // entire match will be 2 numbers
	std::smatch m;
	std::regex_search(str, m, r);  //Searches str, places matches in m, and uses regex r.
	for (auto v : m) std::cout << v << std::endl; //Loops through, displaying all tangable results.
}


This may be misleading, but I was simply testing by using :start and :end. If anyone can find a way to match the contents, but to remove the tags themselves, that would be great.

for the regular expressions themselves, I have looked all over the web for relevant answers with two delimiters, but all of them crash the program post compilation. Please help! Thanks, Drew.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <regex>

int main()
{
    const std::string str("This is just a simple sentence <Bank>hi<Bank> :start hi :end :start bob :end");
    const std::regex re(":start (.*?) :end");

    // http://en.cppreference.com/w/cpp/regex/regex_iterator
    std::sregex_iterator iter( str.begin(), str.end(), re ), end ;
    for( ; iter != end ; ++iter ) std::cout << '\'' << (*iter)[1] << "'\n" ;

    std::cout << '\n' ;

    // http://en.cppreference.com/w/cpp/regex/regex_search
    std::smatch m ;
    for( std::string ss = str ; std::regex_search( ss, m, re ) ; ss = m.suffix() )
    std::cout << '\'' << m[1] << "'\n" ;
}

http://coliru.stacked-crooked.com/a/9b5ab9bcc5154644
Worth mentioning that your input statement looks like some markup language: If you allow arbitrary nesting of tags, then regular expressions won't be sufficient, even allowing for the fact that many real-life engines are substantially more powerful than the theoretical ones.
Last edited on
Topic archived. No new replies allowed.