Qt正则表达式类QRegExp(转)

QRegExp是Qt的正则表达式类.Qt中有两个不同类的正则表达式.第一类为元字符.它表示一个或多个常量表达式.令一类为转义字符,它代表一个特殊字符.一.元字符.匹配任意单个字符.例如,1.3可能是1.后面跟任意字符,再...

QRegExp是Qt的正则表达式类.
Qt中有两个不同类的正则表达式.
第一类为元字符.它表示一个或多个常量表达式.
令一类为 转义字符,它代表一个特殊字符.

一.元字符
.  匹配任意单个字符.例如, 1.3 可能是1. 后面跟任意字符,再跟3
^ 匹配字符串首. 例如, ^12可能是123,但不能是312
$  配字符串尾. 例如, 12$可以是312, 当不能是 123
[] 匹配括号内输入的任意字符.[123]可以为1, 2 或3
*  匹配任意数量的前导字符. 例如, 1*2可以为任意数量个1(甚至没有), 后面跟一个2
+ 匹配至少一个前导字符. 例如, 1+2必须为一个或多个1, 后跟一个2
?  匹配一个前导字符或为空. 例如 1?2可以为2或这12

二.统配模式
通过 QRegExp::setPatternSyntax(QRegExp::Wildcard);可以将元字符设置为统配模式.在统配模式下,只有3个元字 符可以使用.他们的功能没有变化.
? 匹配任意单个字符, 例如, 1?2可以为1,后面跟任意单个字符, 再跟2
* 匹配任意一个字符序列. 例如, 1*2, 可以为1, 后面跟任意数量的字符, 再跟一个2
[] 匹配一个定义的字符集合. 例如, [a-zA-Z.]可以匹配 a到z之间任意一个字符和.  [^a]匹配出小写a以外的字符.

三.转义序列
. 匹配”.”
^ 匹配”^”
$ 匹配”$”
[ 匹配"["
] 匹配”]”
* 匹配”*”
+ 匹配”+”
? 匹配”?”
 匹配响铃字符,使计算机发出嘟的一声.
制表符号
换行符号
  回车符鉿
s  任意空格
xnn 匹配16进制为nn的字符
nn  匹配8进制的nn字符
这些表达式均以开始, 与C++的转义字符相同,所以为了定义QRegExp中的一个转义序列,
需要在前面添加两个\

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

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

{
    QString pattern(“.*=.*”);
    QRegExp rx(pattern);

void testRegexMatch()

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

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

用正则表达式提取数据

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

{
    QString pattern(“(.*)=(.*)”);
    QRegExp rx(pattern);

void testRegexCapture()

    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
    }
}

用正则表达式修改文本

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

    s.replace(QRegExp(“(.*)=”), ”b=”);
    qDebug() << s;                          // b=100

 QString s = ”a=100″;

或是把字符串中匹配的字符串替换”提取的字符串”

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->    QString s = ”a=100″;
    s.replace(QRegExp(“(.*)=(.*)”), ”\1\2=\2″);  // 1 is rx.cap(1), 2 is rx.cap(2)
    qDebug() << s;                                  // a100=100

把正则表达式转换成C/C++ string的小工具

没有Python的”"”或是C#的@。标准的正则表达式因为出现一些特殊字符,在C/C++代码里使用时,必须进行转换。例如:”(S+)s*=s*(S*)”必须转换成”(\S+)\s*=\s*(\S*)”

Qt的SDK里包含一个很帮的GUI工具,可以方便我们进行这类转换并测试你的表达式。在Linux下,它的路径是/usr/local/Trolltech/Qt-4.5.3/examples/tools/regexp/regexp 

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

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

{
    QString pattern(“.*=.*”);
    QRegExp rx(pattern);

void testRegexMatch()

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

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

用正则表达式提取数据

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

{
    QString pattern(“(.*)=(.*)”);
    QRegExp rx(pattern);

void testRegexCapture()

    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
    }
}

用正则表达式修改文本

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

    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;                                  // a100=100

   QString s = ”a=100″;

把正则表达式转换成C/C++ string的小工具

没有Python的”"”或是C#的@。标准的正则表达式因为出现一些特殊字符,在C/C++代码里使用时,必须进行转换。例如:”(S+)s*=s*(S*)”必须转换成”(\S+)\s*=\s*(\S*)”

Qt的SDK里包含一个很帮的GUI工具,可以方便我们进行这类转换并测试你的表达式。在Linux下,它的路径是/usr/local/Trolltech/Qt-4.5.3/examples/tools/regexp/regexp

转自:http://blog.const.net.cn/a/8736.htm

---------------------------------------------------------------自学笔记---------------------------------------------------------------

设置IP地址编辑框输入格式:

QString qStrIPRegExp =
            "((([1-9][0-9]?)|(1[0-9]?[0-9]?)|(2[0-4]?[0-9])|(25?[0-5]))\\.){1}"
            "((0|([1-9][0-9]?)|(1[0-9]?[0-9]?)|(2[0-4]?[0-9])|(25?[0-5]))\\.){2}"
            "(0|([1-9][0-9]?)|(1[0-9]?[0-9]?)|(2[0-4]?[0-9])|(25?[0-5])){1}";
ui->IPAddress->setValidator(new QRegExpValidator(QRegExp(qStrIPRegExp), 0));

 IP地址正则表达式优化:

IP: ((25[0-5]|2[0-4]\d|[01]?\d\d?).){3}(25\d|2[0-4]\d|[01]?\d\d?)

Ps:IP地址中每个数字都不能大于255.

    01.02.03.04 这样前面带有0的数字, 是不是正确的IP地址呢?

    答案: 是的, IP 地址里的数字可以包含有前导 0 (leading zeroes).

原文地址:https://www.cnblogs.com/veins/p/3130646.html