standard RegEx library doesn't recognize spaces? How to fix?

I thought I would attempt to learn the new stuff in C++11. After all, since it was declared to be the new standard, in a few years people less experienced than I am will be learning it as simply part of the C++ language.

Since there aren't very many web sites covering actual examples right now(especially the regex library), all I have for myself is whatever is at http://en.cppreference.com/w/cpp
Even that isn't very well documented, but at least it covers what to expect to be able to do...

I'm using the developer's preview of Visual Studio 2011 to code this.
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
//command-line
#include <iostream>
//work with words; I do not want to type char* when I can merely type string
#include <string>
//Using regular expressions
#include <regex>

using namespace std;
int main()
{
	string numbers;//char numbers[100];//string to get from user
	cout << "Please enter\"a b\".\n";
	cin >> numbers;
	//do not capture!
	//tell me if the user says "a b" including the space.
	regex parser("a ?b");//" ?" means the space does not have to be there.
	if(regex_search(numbers,parser))
	{
		cout << "That is correct.\n";
	}
	else
	{
		cout << "That isn't correct.\n";
	}
	system("pause");
	return 0;
}

I could have made it go in a loop until you said "quit", but that's not what I need in order to understand it. It is very simple to make that loop anyway...
You can type anything, and as long as there is an 'a' char next to a 'b' it should output
That is correct.

However, if I understand regex syntax correctly, entering the text "a b" should also work. It works with http://gskinner.com/RegExr/
All you need to do is type a ?b in the search area of that web page and ab a b in the document area. Both ab and a b should be highlighted.
This does not work in this program for some reason.
In general, the standard regex library doesn't seem to parse the space character (as output by the space bar) correctly. Is this supposed to happen?
Thanks for taking your time to answer!
Replace line 13 with a call to getline().

The string you're feeding to regex_search is "a" when the user enters "a b"

Last edited on
Wow... I feel stupid...
I guess you are right. It was after midnight when I posted that question...
Thank you.
Last edited on
Topic archived. No new replies allowed.