- 论坛徽章:
- 0
|
0到9所有数的正则表达式:[0-9]
0的99所有数的正则表达式:[0-9] | [1-9][0-9]
0的999所有数的正则表达式:[0-9] | [1-9][0-9] | [1-9][0-9][0-9]
........
给定unsigned int n,求0到n的所有数的正则表达式。
- char * func(int x)
- {
- char *re_result=(char *)malloc(MAX_LEN);
- int y=x;
- int round=0;
- //int 对于的10进制数最多15位
- char a[15];
- while(y>1)
- {
- y=(int)(y/10);
- round++;
- }
- itoa(x,a,10);
- a[round]='\0';
- //cout<<"a len : "<<strlen(a)<<"and int array: "<<a<<endl;
- char b09[6]="[0-9]";
- char b19[6]="[1-9]";
- //这个是对比本数小一位的所有数的正则
- // 65536 对应 0-9999
- char re_temp_low[MAX_LEN]={0};
- //这个和本数位数相同、最高位相同的所有数的正则
- // 65536 对应 60000 - 65536
- char re_temp_high[MAX_LEN]={0};
- //这个是与本数相同位数,但是最高位小一些的数的正则,前提是本数最高位大于1
- //65536 对应 10000 - 59999
- char re_start[MAX_LEN]={0};
- int len=round;
- for(;len>=1;len--)
- {
- //计算re_temp_low
- int len_1=len - 2;
- if(len_1>=0)
- {
- if(len_1==0)
- {
- sprintf (re_temp_low,"%s%s",re_temp_low,b09);
- }
- if(len_1>0)
- {
- int len_2=len_1;
- sprintf(re_temp_low,"%s%s",re_temp_low,b19);
- while(len_1>0)
- {
- sprintf(re_temp_low,"%s%s",re_temp_low,b09);
- len_1--;
- }
- }
- sprintf(re_temp_low,"%s | ",re_temp_low);
- //计算 re_temp_high
- int len_high=len - 2;
- //第len_num这个数要单独打印,前面的数直接输出,后面为[0-9]
- int len_num=(round-1)-len_high;
- //cout<<"len_num : "<<len_num<<endl;
- for(int i=0;i<len_num;i++)
- {
- sprintf(re_temp_high,"%s%c",re_temp_high,a[i]);
- }
- char temp[6]="[0-9]";
- //之前a为int数组,用的下面的
- //temp[3]='0'+(a[i]-0);
- temp[3]=a[len_num]-1;
- if(len_num != round-1)
- temp[3]=a[len_num]-1;
- if(len_num == round-1)
- temp[3]=a[len_num];
- sprintf(re_temp_high,"%s%s",re_temp_high,temp);
- while(len_high>0)
- {
- sprintf(re_temp_high,"%s%s",re_temp_high,b09);
- len_high--;
- }
- sprintf(re_temp_high,"%s | ",re_temp_high);
- }
- else break;
- }
- //计算re_start
- if(a[0]>'1')
- {
- char temp[6]="[1-9]";
- char temp2[6]="[0-9]";
- temp[3]=a[0]-1;
- if(a[0] >'2')
- sprintf (re_start,"%s",temp);
- else
- sprintf (re_start,"%s","1");
- for(int i=round;i>0;i--)
- sprintf (re_start,"%s%s",re_start,temp2);
- //cout<<re_start<<endl;
- sprintf (re_result,"%s|%s|%s",re_start,re_temp_high,re_temp_low);
- return re_result;
- }
- sprintf (re_result,"%s|%s",re_temp_high,re_temp_low);
- return re_result;
- }
复制代码 |
|