All pastes #1587328 Raw Edit

Regex with ? and *

public cpp v1 · immutable
#1587328 ·published 2009-10-01 06:29 UTC
rendered paste body
#include <iostream>bool matches(std::string &regex, std::string &test_string){	int i=0, j=0;	while(i<regex.length() && j<test_string.length()){		if(regex[i+1]=='*'){			while(test_string[j]==regex[i]){				j++;			}			i+=2;		}else if(regex[i+1]=='?' ){			if(regex[i]==test_string[j]){				j++;			}			i+=2;		}else if(regex[i]==test_string[j]){			i++;			j++;		}else{			break;		}	}	return (i==regex.length() && j==test_string.length());}int main(int argc, char *argv[]){	std::cout<<std::boolalpha;	std::string regex, test_string;	std::cout<<"Enter the regular expression and test string."<<std::endl;	std::cin>>regex>>test_string; 	std::cout<<"Expressions match : "<<matches(regex, test_string)<<std::endl;	std::cin.get();}