读书人

正则式剔除指定字符串

发布时间: 2012-10-18 13:46:55 作者: rapoo

正则式删除指定字符串
input :0x01 0x02 0x78
output:010278
正则式删除指定字符串,如果用正则式如何写呢?

[解决办法]
你是需要 输入 16进制的数 得到 字符串

char *itoa(int value, char *string, int radix);
radix为16的时候..........
[解决办法]

C/C++ code
#include <iostream>#include <string>using namespace std;int main(){    string s;    int i = 1;    while(true)    {        cin>>i;        if(i == 0)            break;        char ch[5];        itoa(i,ch,16);        if(ch[1] == 0)            s+='0';        s+=ch;    }    cout<<s<<endl;    return 0;}
[解决办法]


#include <iostream>
#include <string>
using namespace std;

int main()
{

string s;
while(true)
{
int i = 0;
char ch[10];
scanf("%s",ch);
if( strlen(ch) <= 2)
break;

if(ch[i++] == '0')
{
if(ch[i++] == 'x')
{
for(int j = i;j < strlen(ch);j++)
s+=ch[j];

}
}
}
cout<<s<<endl;
return 0;
}

在最后我需要一个 结束输入的.......
input :0x01 0x02 0x78 0
output:010278
[解决办法]
源字符串:"0x01 0x02 0x78"
查找用正则表达式:" ?0x"
替换为:""
全部替换后结果:"010278"

推荐使用regtest软件
[解决办法]
input :0x01 0x02 0x78
output:010278
正则式删除指定字符串
---------------------
其实不需要删除,只需要找到0x后面的数,输出就是了
Python code
>>> import re>>> string = '0x01 0x02 0x78 正则表达式0xff 3456700A'>>> patten = '0x[0-9a-zA-Z]{2}' #正则表达式>>> alist = re.findall(patten,string)>>> str = ''>>> for i in alist:...     str+=i[2:]...>>> print str010278ff>>> 

读书人网 >C++

热点推荐