`

QT正则表达式的使用

    博客分类:
  • QT
 
阅读更多

http://www.cnblogs.com/frankbadpot/archive/2009/10/18/1583617.html

 

1. 用正则表达式验证文本有效性

你可以使用QRegExp::exactMatch来判断一个字符串是否符合一个pattern。 

 

void testRegexMatch()
{
    QString pattern(".*=.*");
    QRegExp rx(pattern);

    bool match = rx.exactMatch("a=3");
    qDebug() << match;                      // True

    match = rx.exactMatch("a/2");
    qDebug() << match;                      // False
}

 

2. 用正则表达式提取数据

你可以利用利用正则表达式从一个字符串里提取特定的字段或数据。例如,你可以用以下代码从"a=100"里提取"a"和"100"。

 

void testRegexCapture()
{
    QString pattern("(.*)=(.*)");
    QRegExp rx(pattern);

    QString str("a=100");
    int pos = str.indexOf(rx);              // 0, position of the first match.
                                            // Returns -1 if str is not found.
                                            // You can also use rx.indexIn(str);
    qDebug() << pos;
    if ( pos >= 0 )
    {
        qDebug() << rx.matchedLength();     // 5, length of the last matched string
                                            // or -1 if there was no match
        qDebug() << rx.capturedTexts();     // QStringList("a=100", "a", "100"),
                                            //   0: text matching pattern
                                            //   1: text captured by the 1st ()
                                            //   2: text captured by the 2nd ()

        qDebug() << rx.cap(0);              // a=100, text matching pattern
        qDebug() << rx.cap(1);              // a, text captured by the nth ()
        qDebug() << rx.cap(2);              // 100,

        qDebug() << rx.pos(0);              // 0, position of the nth captured text
        qDebug() << rx.pos(1);              // 0
        qDebug() << rx.pos(2);              // 2
    }
} 

 

3. 用正则表达式修改文本

你可以把字符串中匹配的字符串替换成"一般字符串"

 

QString s = "a=100";
s.replace(QRegExp("(.*)="), "b=");
qDebug() << s;                          // b=100
 或是把字符串中匹配的字符串替换"提取的字符串"

 

 

QString s = "a=100";
s.replace(QRegExp("(.*)=(.*)"), "\\1\\2=\\2");  // \1 is rx.cap(1), \2 is rx.cap(2)
qDebug() << s;  

 

 

http://www.thisisqt.com/forum/viewthread.php?tid=239

Regexp是由表达式,量词和声明组成的。最简单的表达式是一个字符,如x或是5。一个表达式也可以是一个用方括号包裹的字符集。[ABCD]将会匹配一个A或者一个B或者一个C或者一个D。我们也可以写成[A-D],这跟[ABCD]所要表达的是一样的意思。而[A-Z]则可以匹配任何一个大写字母。
一个量词指明表达式出现的次数。x{1,1} 匹配一个但仅仅一个x。x{1,5} 意味着匹配至少一个但不超过5个的x。

如果你看到其他地方的regexp,它们可能看起来跟上面有所不同,这是因为一个字符集和量词太常用了以至于它们也可以通过其他的字符来表达。如[0-9] 可以用\d来代替。而{1,1}来表示出现次数则可以用字符本身即可,如x{1,1}和x是一样的。

如果想匹配一个单词,如mail,我们可以写成m{1,1}a{1,1}i{1,1}l{1,1},但由于字符表达式数量默认为1,因此我们也可以就直接写成mail。现在我们来看看匹配多个单词的情况。如果我们想同时匹配mail或者letter或者correspondence又该怎么写呢?这里我们需要用到|符号,我们可以写成mail|letter|correspondence,这个表达式可以匹配这三个单词,但同时它也会匹配我们不想要的单词,如email。为了防止这种情况,我们需要说明单词的边界。

首先我们用圆括号将我们的表达式包裹起来,(mail|letter|correspondence)。圆括号可以将我们的表达式聚合起来。其次我们还需要使用\b单词边界声明

 

http://blog.csdn.net/daysummer/article/details/561038

c 匹配字符 'c' 

. 匹配任意字符 
^ 匹配一个输入的开始
$ 匹配一个输入的结束 
[] 匹配一个字符串集的定义 - see below. 
a* matches a sequence of zero or more a's (可以是a,ab,ade等;*可以代表任意个字符,包括0个)
a+ matches a sequence of one or more a's  (可以是ab,ade,aeee等,但不能是a;+只代表一个以上的字符)
a? matches an optional a     匹配可选的a  (可以是ab,ac,ad等,但不能是a,或ade;?只代表一个字符)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics