qt之正则表达式
原地址:http://blog.csdn.net/phay/article/details/7304455
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以外的字符.
三.转义序列
\. 匹配”.”
\^ 匹配”^”
\$ 匹配”$”
\[ 匹配"["
\] 匹配”]”
\* 匹配”*”
\+ 匹配”+”
\? 匹配”?”
\b 匹配响铃字符,使计算机发出嘟的一声.
\t 制表符号
\n 换行符号
\r 回车符鉿
\s 任意空格
\xnn 匹配16进制为nn的字符
\0nn 匹配8进制的nn字符
这些表达式均以\开始, 与C++的转义字符相同,所以为了定义QRegExp中的一个转义序列,
需要在前面添加两个\\
用正则表达式验证文本有效性
你可以使用QRegExp::exactMatch来判断一个字符串是否符合一个pattern。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->void testRegexMatch()
{
QString pattern(“.*=.*”);
QRegExp rx(pattern);
bool match = rx.exactMatch(“a=3″);
qDebug() << match; // True
match = rx.exactMatch(“a/2″);
qDebug() << match; // False
}
用正则表达式提取数据
你可以利用利用正则表达式从一个字符串里提取特定的字段或数据。例如,你可以用以下代码从”a=100″里提取”a”和”100″。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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
}
}
用正则表达式修改文本
你可以把字符串中匹配的字符串替换成”一般字符串”
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> QString s = ”a=100″;
s.replace(QRegExp(“(.*)=”), ”b=”);
qDebug() << s; // b=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。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->void testRegexMatch()
{
QString pattern(“.*=.*”);
QRegExp rx(pattern);
bool match = rx.exactMatch(“a=3″);
qDebug() << match; // True
match = rx.exactMatch(“a/2″);
qDebug() << match; // False
}
用正则表达式提取数据
你可以利用利用正则表达式从一个字符串里提取特定的字段或数据。例如,你可以用以下代码从”a=100″里提取”a”和”100″。
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->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
}
}
用正则表达式修改文本
你可以把字符串中匹配的字符串替换成”一般字符串”
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> QString s = ”a=100″;
s.replace(QRegExp(“(.*)=”), ”b=”);
qDebug() << s; // b=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
qt之正则表达式的更多相关文章
- Qt:正则表达式语法:
正则表达式是验证输入.从输入中提取数据以及对输入进行搜索和替换的强大工具,所谓正则表达式,regexp是一种利用模式匹配语言来描述字符串组成限制条件的方式; Qt 提供了一个Q ...
- [QT]构建正则表达式测试
正则表达式是个强大的东西 暂时先记录一个用法: QString str = "Peak memory: KEY s"; QString data = "Peak memo ...
- QT中正则表达式的简单说明
使用方法: QRegExp acNumRE("[0-9]{19}"); lineEdit->setValidator(new QRegExpValidator(acNumRE ...
- Qt使用正则表达式去掉小数位多余的0
QRegExp rx; rx.setPattern("(\\.){0,1}0+$"); double double01 = 15648.120000; double double0 ...
- QRegExp 正则表达式详解
引言 正则表达式(regular expression)就是用一个“字符串”来描述一个特征,然后去验证另一个“字符串”是否符合这个特征.比如 表达式“ab+” 描述的特征是“一个 'a' 和 任意个 ...
- qt QRegExp使用(搬运工)
设置正则表达式. 类似下面的 QRegExp 这里的用法就是用来检测QString等字符串错误的,例如文件名里面最好就不出现<>|/\:等,所以可以如下定义QRegExp rx(" ...
- Qt——正则表达式
在项目中经常会遇到对字符串进行操作的情况,我们可以直接使用QString的一些函数,但QT提供了一个更加强大的类——QRegExp,使用正则表达式来操作字符串. 先说说我最近遇到的几个问题: 1.对输 ...
- QT正则表达式---针对IP地址
判断合法IP的QT正则表达式: bool IsIPaddress(QString ip) { QRegExp rx2("(//d+)(//.)(//d+)(//.)(//d+)(//.)(/ ...
- QT正则表达式
QT正则表达式有一个问题,当初始状态是不符合正则表达式时,能够输入任意字符,若在输入过程中符合正则表达式,马上进入字符检测状态,即只能接受符合正则表达式的字符.
随机推荐
- 【工具篇】利用DBExportDoc V1.0 For MySQL自动生成数据库表结构文档
对于DBA或开发来说,如何规范化你的数据库表结构文档是灰常之重要的一件事情.但是当你的库,你的表排山倒海滴多的时候,你就会很头疼了. 推荐一款工具DBExportDoc V1.0 For MySQL( ...
- Unity KillCount
using UnityEngine; using System.Collections; public class KillCountMult : MonoBehaviour { public GUI ...
- 数矩形(N - 暴力求解、打表)
数矩形 Description 给你一个高为n ,宽为m列的网格,计算出这个网格中有多少个矩形,下图为高为2,宽为4的网格. Input 第一行输入一个t, 表示有t组数据,然后 ...
- zIndex属性在IE中无效
在ie中他的子类的zindex就以父类为准: <!doctype html> <html> <head> <meta charset="utf-8& ...
- 基于FPGA的DW8051移植(二)
基于上一篇博文继续,本来想换到oc8051,但是还是不甘心,弄了这么久还是没有弄出来,真是打击屎了. 上一篇说3f进入了operation code所以判断是代码错误,后来发现不可以这么判断. 因为地 ...
- 在Struts 2中实现IoC
IoC(Inversion of Control,以下译为控制反转)随着Java社区中轻量级容器(Lightweight Contianer)的推广而越来越为大家耳熟能详.在此,我不想再多费唇舌来解释 ...
- Linux 特殊符号使用: 倒引号`的使用
Linux中有很多特殊符号,这里介绍 ` 倒引号的含义. 我们考虑下这个场景,有时我们需要将一个命令的执行结果赋值给某个变量,或者别的用途. 这时我们可以用两个`倒引号将该命令括起来. 例1: 如 e ...
- nginx使用小记
中文wiki社区:http://wiki.codemongers.com/NginxChs 一 . nginx安装 1. 下载nginx : http://sysoev.ru/nginx/downlo ...
- Dreamer2.1 发布 新增将Bean解析成xml和json
一个上午,增加两个功能 1.直接将对象解析成XML 2.将对象解析成JSON 对象可以是数组,可以是集合,也可以是单个对象 源码和jar下载地址:http://pan.baidu.com/share/ ...
- BZOJ 2875: [Noi2012]随机数生成器( 矩阵快速幂 )
矩阵快速幂...+快速乘就OK了 ----------------------------------------------------------------------------------- ...