Chinaunix首页 | 论坛 | 博客
  • 博客访问: 568672
  • 博文数量: 97
  • 博客积分: 5090
  • 博客等级: 大校
  • 技术积分: 969
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-01 14:56
文章分类

全部博文(97)

文章存档

2011年(1)

2009年(1)

2008年(14)

2007年(37)

2006年(44)

我的朋友

分类: LINUX

2008-04-23 14:02:16

#ifdef WIN32
#include "stdafx.h"
#endif
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class CPickupEmail
{
public:
    CPickupEmail(void) {}
    ~CPickupEmail(void) {}
    void PickupEmail(const char* szBuf, const char* szFlag)
    {
        m_strEMailList.clear();
        char *pFlag = strstr(szBuf, szFlag);
        char *pStart = pFlag;
        while ( pFlag != NULL )
        {
            char ch = *(--pFlag);
            while ( pFlag != NULL && ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_' ) )
            {
                ch = *(--pFlag);
            }
            char szEmail[128];
            memset((void *)szEmail, 0, sizeof(szEmail));
            int i = 0;
            ch = *(++pFlag);
            while ( true )
            {
                if ( (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_' || ch == '.' || ch == '@' )
                {
                    szEmail[i++] = ch;
                    ch = *(++pFlag);
                }
                else if ( strstr(pFlag, szFlag) == pFlag )
                {
                    szEmail[i++] = '@';
                    pStart += strlen(szFlag);
                    ch = *(++pStart);
                }
                else if ( strstr(pFlag, ".") == pFlag )
                {
                    szEmail[i++] = '.';
                    pFlag += 4;
                    ch = *(++pFlag);
                }
                else
                    break;
            }
            pFlag = strstr(pFlag, szFlag);
            pStart = pFlag;
            if ( !IsExists(szEmail) )
                m_strEMailList.push_back(szEmail);
        }
    }

    friend ostream& operator<<(ostream &out, CPickupEmail& EMail)
    {
        for ( vector<string>::const_iterator it = EMail.m_strEMailList.begin(); it != EMail.m_strEMailList.end(); it++ )
        {
            out << (*it) << endl;
        }
        return out;
    }

private:
    bool IsExists(const char* szEMail)
    {
        for ( vector<string>::const_iterator it = m_strEMailList.begin(); it != m_strEMailList.end(); it++ )
        {
            if ( (*it) == szEMail )
                return true;
        }
        return false;
    }

private:
    vector<string> m_strEMailList;
};

int main(int argc, char* argv[])
{
    CPickupEmail test;
    char szBuf[] = "How to pick up E-mail address from a string?luojiafeng1984@sina.com ";
    test.PickupEmail(szBuf, "@");
    cout << test;
    return 0;
}

阅读(1686) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~