QRegExp

2 views
Skip to first unread message

Daniel Akbari

unread,
Jun 26, 2011, 4:08:49 AM6/26/11
to advprg_892
سلام
می شه خواهش کنم یکم راجع به
QRegExp
توضیح بدین
مثلا

QRegExp messageRegex("^([^:]+):(.*)$")
messageRegex.indexIn(line)
messageRegex.cap(1)
چیکار می کنه

ممنون

saeed dadkhah

unread,
Jun 26, 2011, 4:35:59 AM6/26/11
to advpr...@googlegroups.com
سلام
Regular Expressions
یکی از بهترین وسیله ها برای تست کردن مقادیر ورودی توسط کاربر و یا جست و جو در یه متنه
پس اگه شما قصد دارین که مثلا وقتی از کاربر در خواست میکنید ایمیلشو بزنه اولش
www
نزنه باید با عبارات با قاعده این رو تست کنید .
یا مثلا وقتی میخواهید توی یه متنی دنبال یه کلمه بگردید که با مثلا
d
شروع شده و با
m
ختم شده باید از عبارات با قاعده استفاده کنید . چرا به اینا میگند عبارات با قاعده ؟
به یه آدرس ایمیل نگاه کن
اولش بایه سری حرف شروع میشه بعدش علامت
@
بعدش یه سری حرف و یه نقطه و یه سری حرف دیگه
به همین دلیل بهش میگن عبارات با قاعده
 
اما این چیزی که شما نوشتی معنیش اینه
دنبال عبارتی هستیم که با تعداد یک یا بیشتر از کاراکتر هایی به غیر از دو نقطه شروع بشه
بعدش یه دونقطه بیاد
بعدش هرچی دلش خواست کاراکتر بیاد صفر یا بیشتر
پس رشته های زیر با اون مطابقت دارند
 
saeed:dadkhah
saeed:
saeed:dadkhah:sfsd:sdf 
 
ولی رشته های زیر نه
:saeed
saeed
 
تابع
indexIn
یه رشته میگیره و سعی میکنه اندیس اولین کاراکتر رشته ای که مطابق الگوی شما ست بر گردونه
 
برای یاد گیری عبارات با قاعده آدرس زیر مفیده
2011/6/26 Daniel Akbari <danit...@gmail.com>

saeed dadkhah

unread,
Jun 26, 2011, 4:47:11 AM6/26/11
to advpr...@googlegroups.com
در مسیر زیر میتونید یه مثال ببینید که خیلی کاربردیه و استفاده زیادی مخصوصا در حین یادگیری عبارات با قاعده خواهد داشت
Qt Examples And Demos -> Tools -> Regular Expressions

Daniel Akbari

unread,
Jun 26, 2011, 5:20:51 AM6/26/11
to advpr...@googlegroups.com
thanks

Daniel Akbari

unread,
Jun 26, 2011, 6:22:25 AM6/26/11
to advpr...@googlegroups.com
خروجی ها تابع 
index 
چطورین
-1 , -2 , 0 
چه معنی دارند؟

saeed dadkhah

unread,
Jun 26, 2011, 6:23:43 AM6/26/11
to advpr...@googlegroups.com

int QRegExp::indexIn ( const QString & str, int offset = 0, CaretMode caretMode = CaretAtZero ) const

Attempts to find a match in str from position offset (0 by default). If offset is -1, the search starts at the last character; if -2, at the next to last character; etc.

Returns the position of the first match, or -1 if there was no match.

The caretMode parameter can be used to instruct whether ^ should match at index 0 or at offset.

You might prefer to use QString::indexOf(), QString::contains(), or even QStringList::filter(). To replace matches use QString::replace().

Example:

 QString str = "offsets: 1.23 .50 71.00 6.00";
 QRegExp rx("\\d*\\.\\d+");    // primitive floating point matching
 int count = 0;
 int pos = 0;
 while ((pos = rx.indexIn(str, pos)) != -1) {
     ++count;
     pos += rx.matchedLength();
 }
 // pos will be 9, 14, 18 and finally 24; count will end up as 4

Although const, this function sets matchedLength(), capturedTexts() and pos().

If the QRegExp is a wildcard expression (see setPatternSyntax()) and want to test a string against the whole wildcard expression, useexactMatch() instead of this function.

See also lastIndexIn() and exactMatch().



2011/6/26 Daniel Akbari <danit...@gmail.com>
Reply all
Reply to author
Forward
0 new messages