• 9443阅读
  • 1回复

通过正则表达式(QRegExp),从QString中提取所有E-mail地址 [复制链接]

上一主题 下一主题
离线fengtian
 
只看楼主 倒序阅读 楼主  发表于: 2007-05-31
— 本帖被 XChinux 执行加亮操作(2008-07-18) —
在QString中可能有多个E-mail地址   我要怎么才能把他们都获得呢??

我现在尝试过的一些:

QString str="my name is bob,bobis good boy,i lick bob<";
QRegExp reg("bob");
reg.indexIn(str);
qDebug() << str.count(reg); // output: 3  OK


reg.indexIn(str);
reg.cap(0); // output: "bob" OK
reg.cap(1); // output: ""  why
reg.cap(2); // output: ""  why

-------------------------------------------------------

QString str="my name is bob,bobis good boy,i lick bob<";
QRegExp reg("bob");
reg.indexIn(str);
QStringList list=reg.capturedTexts();
qDebug() << list.count(); // output : 1
[ 此贴被XChinux在2008-07-18 16:48重新编辑 ]
离线htyoung

只看该作者 1楼 发表于: 2007-06-01
给个简单的实现,详细的用法请多看 QRegExp类的说明,qt的help里有一个专门的章节讲正则表达式

PS:这个例子不是很好,没通用性,只能做参考。

QString str="hello1@sina.com;hello2@sina.com";

QRegExp reg"(\\w+\\@\\w+\\w*\\.\\w+)\\;(\\w+\\@\\w+\\.\\w+)";
reg.indexIn(str);



reg.indexIn(str);

reg.cap(1); // hello1@sina.com

reg.cap(2); // hello2@sina.com
***
QT5
***
快速回复
限100 字节
 
上一个 下一个