PDA

View Full Version : How to get a QString from a file (use regular expression)



fengtian.we
30th May 2007, 10:20
My case:
I hava a file: a.txt

a e-mail address is in the file.

how can i get the e-mail String??

jpn
30th May 2007, 10:42
QFile, QTextStream

fengtian.we
30th May 2007, 10:55
Now , I find a: bool QString::contains(const QRegExp & rx ) const

but it is return a bool value~ I need QStrings

marcel
30th May 2007, 10:57
But with the classes Jpn suggested you can read QStrings from a file.
Read the docs to find out how you can do that.

Regards

fengtian.we
30th May 2007, 11:37
no Class QString is not supply any method to get it.

marcel
30th May 2007, 11:50
Are you sure?
What about this:


QFile file("somefile.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream textStream( &file);
QString fileContents = textStream.readAll();


You can also read the stream in chunks, if the file is too big.

Regards

high_flyer
30th May 2007, 12:05
And here is a nice RegEx for email matching:
http://www.regular-expressions.info/email.html

fengtian.we
30th May 2007, 12:08
yes , I can ,I can read the file now,

now , this string is a big string, a e-mail address in the string ,

I want get the e-mail address use regular expressions.. How TO

high_flyer
30th May 2007, 12:20
READ my last post.

fengtian.we
30th May 2007, 12:54
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

high_flyer
30th May 2007, 13:25
Because I think you missunderstood the docs:

Each subsequent element corresponds to the next capturing open left parentheses. Thus cap(1) is the text of the first capturing parentheses, cap(2) is the text of the second, and so on.
So QRegEx::cap(int n) does not retrieve the n'th match, but the match of the n'th parentheses in your expression, and you have only one (n=0) which is why only the first cap() returns a non empty string.

What you are looking for is QRegExp::capturedTexts() I think.

fengtian.we
30th May 2007, 16:44
I try it (QRegExp::capturedTexts() ) ,

find one ....

high_flyer
30th May 2007, 16:53
what do you mean?
in your last post count() returned 3...
Better if you show your code, and not only explain the end effect.

fengtian.we
31st May 2007, 04:36
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

high_flyer
31st May 2007, 10:37
After a closer reading, it turns out capturedTexts() is not the right method you are looking for:

Some regexps can match an indeterminate number of times. For example if the input string is "Offsets: 12 14 99 231 7" and the regexp, rx, is (\d+)+, we would hope to get a list of all the numbers matched. However, after calling rx.indexIn(str), capturedTexts() will return the list ("12", "12"), i.e. the entire match was "12" and the first subexpression matched was "12". The correct approach is to use cap() in a loop.

fengtian.we
31st May 2007, 11:13
I'm read the Qt Assistant yesterday, I thinks should be :


QString str="bob's msn is aa@g.com,and bob's email is qq@aaaa.com.cn :)";
QRegExp reg("[A-z0-9_\.-]+@[A-z0-9\.]+");

int count=str.count(reg);
int from=0;
int in=0;
QStringList rs;

for(int i=0;i<count;i++)
{
in=reg.indexIn(str,from);
rs.append(reg.cap(0));
from=in+rs.value(i).size();
}


qDebug() << rs.count(); //output 4(aa@g.com,a@g.com,qq@aaaa.com.cn,q@aaaa.com.cn)

qDebug() << rs.value(0); //output "aa@g.com"
qDebug() << rs.value(1); //output "qq@aaaa.com.cn"
qDebug() << rs.value(2); //output ""
qDebug() << rs.value(3); //output ""




how about this :) is that ok?

and please check my regular expressions"[A-z0-9_\.-]+@[A-z0-9\.]+".

fengtian.we
31st May 2007, 12:06
OK!!!! Thank you everyone!

this is my way:




QString str="bob's msn is aaa@g.com,and bob's email is qq@aaaa.com.cn :)";
QRegExp reg("[A-z0-9_\.-]+@[A-z0-9\.]+");

int startPos=0;
int mailPos=0;
QStringList rs;

while(true)
{
mailPos=reg.indexIn(str,startPos);
if(mailPos<0)
{
break;
}
rs.append(reg.cap(0));
startPos=mailPos+rs.last().size();
}

qDebug() << rs.count(); // 2
qDebug() << rs.value(0); // "aaa@g.com"
qDebug() << rs.value(1); // "qq@aaaa.com.cn"


haha :D :D