在分析某个C++源文件的过程中,有这样一行变量定义式:
int t_from=20,t_to=12,t_length=120,i_count,t_usetime;
现在你并不知道这行一共定义了多少个变量,以及哪些变量进行了初始值设定,要求从该语句中解析出所有以“t_”开头的变量名,并且如果该变量赋予了初始值的话也需要提取出来,请构造一个方法来实现该要求。
#include<iostream>
#include<string>
#include"boost/regex.hpp"
using namespace std;
using namespace boost;
int main()
{
FILE *fp=fopen("E:\\正则.txt","r");
regex reg_1("t_[a-z]{1,}");
regex reg_2("t_[a-z]{1,}=[0-9]{1,}");
char s[100];
int t_count=0;
while((fscanf(fp,"%s",s))!=EOF)
{
if(regex_match(s,reg_1)==true)
{
//cout<<"开始"<<endl;
t_count++;
//cout<<s<<endl;
}
if(regex_match(s,reg_2)==true)
{
t_count++;
string str=s;
int len=str.length();
cout<<str<<"的长度为:"<<len<<endl;
int pos=str.find('=',0);
cout<<str<<"中的数字为:"<<str.substr(pos+1,len-pos-1)<<endl;
}
}
cout<<"以t_开头的总数为:"<<t_count<<endl;
fclose(fp);
system("pause");
return 0;
}