16,472
社区成员
发帖
与我相关
我的任务
分享
Dim reg,matchs
Set reg=New RegExp
reg.Pattern="(\d+)\.(\d+)\.(\d+)\.(\d+)"
Set matchs=reg.Execute("fjkdlsaj192.168.1.39iwiofda")
WScript.Echo matchs(0).Value '输出192.168.1.39
For Each match In matchs
WScript.Echo match.Submatches.Item(0) '输出192
WScript.Echo match.Submatches.Item(1) '输出168
WScript.Echo match.Submatches.Item(2) '输出1
WScript.Echo match.Submatches.Item(3) '输出39
Next
运行结果如下:
192.168.1.39
192
168
1
39
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR buff[20]={0};
CString line=_T("fjdlau192.168.1.39jfkdlas");
CString pattern=_T("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)");
CAtlRegExp<> reg;
int status=reg.Parse(pattern);
CAtlREMatchContext<> matchs;
BOOL b=reg.Match(line,&matchs);
if(b)
{
int nBytes=(DWORD)matchs.m_Match.szEnd-(DWORD)matchs.m_Match.szStart; //得到匹配字节数
::memcpy_s(buff,30,matchs.m_Match.szStart,nBytes); //复制匹配到buff
}
return 0;
}