Regex pattern

Hi everyone.

I am reading Stroustrup's book and particularly the chapter regarding Regex. I have an example in the book where he shows, by designing a Regex pattern, to grab and import US ZIP codes from .txt file.

The regex pat("\\w{2}\\s*\\d{5}(-\\d{4})?") he's using should output this:

1
2
3
4
5
6
7
8
9
10
pattern: "\w{2}\s*\d{5}(–\d{4})?" 
1: TX77845
2: tx 77843
5: TX23456–3456
      : –3456
6: TX77845–1234
      : –1234
7: Tx77845
8: TX12345–1234
      : –1234


But when I run mine, it is composed only by main pattern, without the sub-pattern. I saw he uses Boost version of Regex while I use MS Visual Studio 2015 Community with the default one. How is this possible? Even this std version is similar to Perl's Regex as Stroustrup states in his book?

Code:
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
34
35
36
37
38
39
40
41
42
#include "stdafx.h"
#include <regex>
#include <iostream>
#include <stdexcept>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	try
	{
		ifstream in("file.txt");    // input file
		if (!in) cerr << "no file\n";

		regex pat("\\w{2}\\s*\\d{5}(-\\d{4})?");    // ZIP code pattern
		// cout << "pattern: " << pat << '\n';

		int lineno = 0;
		string line;    // input buffer
		while (getline(in, line)) {
			++lineno;
			smatch matches;    // matched strings go here
			if (regex_search(line, matches, pat)) {
				cout << lineno << ": " << matches[0] << '\n';
				if (1<matches.size() && matches[1].matched)
					cout << "\t: " << matches[1] << '\n';    // sub-match
			}
		}
	}
	catch (exception& e) {
		cerr << "error: " << e.what() << '\n';
		return 1;
	}
	catch (...) {
		cerr << "Oops: unknown exception!\n";
		return 2;
	};

    return 0;
}


Input file "file.txt":

address TX77845
ffff
tx 77843 asasasaa
ggg
TX3456–23456
howdy
zzz
TX23456–3456sss
ggg TX33456–1234
cvzcv TX77845–1234
sdsas
xxxTx77845xxx
TX12345–123456

P.S. I had to comment the cout << "pattern: " << pat << '\n'; because I had to overload operator <<. How could I overload it and print Regex pattern?

Thanks
Last edited on
Issue solved! It was because I typed a "-" char code 45 while in text there was the "-" char code 8211.
Topic archived. No new replies allowed.