Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I'm trying to return an array of matches using this code:
std::smatch sm;

if (regex_search(content, sm, regex("[.+]")))
{
	for (int i = 1; i < static_cast<int>(sm.size()); i++)
	{
		cout << sm[i] << endl;
	}
}


The string to match comes in this format:

[
	name: bin1
	type: static
	px: 14
	py: 27
	pz: 93
	rx: 45
	ry: 0
	rz: 0
]
[
	name: car1
	type: static
	px: 14
	py: 27
	pz: 93
	rx: 45
	ry: 0
	rz: 0
]


I tried using the regex "/[.+]/s" to allow for multiline, but the compiler keeps complaining about the "[" and "/" symbols being unrecognised. When I double escape them, the error goes away, but the regex returns nothing.

With the code I have at the moment, nothing appears in the console.

What I have tried:

Everything I could think of, so now I'm here
Posted
Updated 17-Dec-17 8:08am
v3
Comments
PIEBALDconsult 17-Dec-17 11:35am    
Use a BACKSLASH (\) -- not a SLASH (/) -- to escape the BRACKETs.

Try, for instance
C++
#include <iostream>
#include <regex>
using namespace std;
int main()
{
  std::smatch sm;

  string content ="[\nname: bin1\ntype: static\npx: 14\n]\n[\nname: car1\ntype: static\npx: 14\n]";

  while (regex_search(content, sm, regex("\\[([^\\]]+)\\]")))
  {
    cout << sm[1] << endl;
    content = sm.suffix();
  }

}
 
Share this answer
 
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 
Comments
[no name] 17-Dec-17 14:22pm    
I did actually use RegExr and Debuggex, both worked fine but the regex patterns didn’t work in C++
Patrice T 17-Dec-17 14:57pm    
There is more that 1 flavor of regex and language C++ have specific rules for char strings (as you can see in solution 1).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900