寻找文本中的ip地址(正则表达式)

#include<iostream>
#include<cstdio>
#include<regex>
using namespace std;
int main(){
	regex reg1("(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])");
	freopen("ipcheck.txt","r",stdin);
	smatch r1;
	string str;
	while(cin>>str && str!="$"){
//		cout<<str<<endl;
//		cout<<regex_match(str,r1,reg1)<<endl;
		if(regex_match(str,r1,reg1)){
			cout<<r1.str()<<" is an ip."<<endl;
		}
	}
	string total="";
	string t;
	while(getline(cin,t)){
		total+=t;
	}
	cout<<total<<endl<<"$"<<endl;
	string::const_iterator itr_beg =total.begin();
	string::const_iterator itr_end =total.end();
	int cnt=0;
	while(regex_search(itr_beg,itr_end ,r1,reg1)){
		cout<<r1.str()<<endl;
		cnt++;
		itr_beg=r1[0].second;
	}
	cout<<"Total : "<<cnt<<endl;
	return 0;
}
//((25[0-5])|(2[0-4]\\d)|((1\\d{2})|([1-9]?\\d)))

ipcheck.txt:

118.132.32.21
$
Reads wide characters from stream and stores them as a C wide string into ws until (num-1) characters have been read or either a newline or the 118.132.32.21 end-of-file is reached, whichever happens first.
A newline wide character makes fgetws stop reading, but it is considered 118.112.32.21a valid character by the function and 118.132.32.21included in the string copied to ws.

118.132.32.21A terminating null character is automatically appended after the characters copied to ws.

The external representation of wide characters in files are multibyt118.132.32.21e characters:118.132.32.21 These are translated as if mbrtowc was called (using the 108.132.32.21stream's internal mbstate_t object).
228.132.32.21
118.132.32.21The function behaves as if fgetwc was used to read the characters from the stream.

118.132.32.21This is the wide c118.132.32.21haracter equivalent of fgets (<cstdio>).

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值