All pastes #124033 Raw Edit

Stuff

public text v1 · immutable
·published 2006-08-09 19:51 UTC
rendered paste body
 
#include <iostream>
#include <string>

using namespace std;

#include <pcre.h>


int main()
{
	string str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
	string pattern = "^(([rRnNbBqQkKpP1-8]{1,8}/){7,7}[rRnNbBqQkKpP1-8]{1,8}) ([wb]) ([KQkq]{1,4}|-) ([a-h][1-8]|-) ([\\d]) ([\\d])";
	
	pcre *re;
	int options = 0;
	const char * error;
	int erroffset;
	re = pcre_compile ((char *) pattern.c_str(), options, &error, &erroffset, NULL);

	if(! re)
	{
		cerr << "Failed" << endl;
	}
	else
	{
		int count = 0;
		int ovector[24];

		count = pcre_exec (re, NULL, (char *) str.c_str(), str.length(), 0, 0, ovector, 24);
		cout << "Count: " << count << endl;
		if(! count)
		{
			cerr << "No match" << endl;
		}
		else
		{
			/**
			0: 0/56: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
			2: 0/43: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR
			4: 26/35: PPPPPPPP/
			6: 44/45: w
			8: 46/50: KQkq
			10: 51/52: -
			12: 53/54: 0
			14: 55/56: 1
			*/
	
			cout << str.substr(ovector[2], ovector[2 + 1] - ovector[2]) << endl;
			cout << str.substr(ovector[6], ovector[6 + 1] - ovector[6]) << endl;
			cout << str.substr(ovector[8], ovector[8 + 1] - ovector[8]) << endl;
			cout << str.substr(ovector[10], ovector[10 + 1] - ovector[10]) << endl;
			cout << str.substr(ovector[12], ovector[12 + 1] - ovector[12]) << endl;
			cout << str.substr(ovector[14], ovector[14 + 1] - ovector[14]) << endl;
		}
	}
	
	cout << endl;
	return 0;
}