加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Qt的正则表达式

发布时间:2020-12-14 04:17:29 所属栏目:百科 来源:网络整理
导读:如果使用的是QtQuick的正则表达式,请读我以前的博客:http://www.jb51.cc/article/p-fqrwgbye-up.html。 首先明白什么是正则表达式:正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、
如果使用的是QtQuick的正则表达式,请读我以前的博客:http://www.52php.cn/article/p-fqrwgbye-up.html。


首先明白什么是正则表达式:正则表达式使用单个字符串来描述、匹配一系列符合某个句法规则的字符串。在很多文本编辑器里,正则表达式通常被用来检索、替换那些符合某个模式的文本。正则表达式由一些普通字符和一些元字符组成。像基本的我就不说了,想知道的:http://baike.baidu.com/link?url=5TtstGps4OZ9UEplfDL93ThRTw5ZrUrCyWFPK7R_Y4-xPVK37eA8PBdyrGjTYm3wot30b71YKLUxN02ausof7_。

下面我说一下Qt中如何用正则(QRegExp、QIntValidator):


Qt文档中给了一个简单的例子:

--------------------------------------------------------------------------------------------

QRegExp rx("(d+)");
QString str = "Offsets: 12 14 99 231 7";
QStringList list;
int pos = 0;

while ((pos = rx.indexIn(str,pos)) != -1) {
list << rx.cap(1);
pos += rx.matchedLength();
}
// list: ["12","14","99","231","7"]


QValidator *validator = new QIntValidator(100,999,this);
QLineEdit *edit = new QLineEdit(this);

// the edit lineedit will only accept integers between 100 and 999
edit->setValidator(validator);

------------------------------------------------------------------------------------------

简单写一些几种使用情景,这里举例使用QLineEdit作为显示框:
1、整型(只能输入数字)

QRegExp regx("[0-9]+$");
QValidator *validator = new QRegExpValidator(regx,ui->lineEdit_user);
ui->lineEdit_user->setValidator(validator);

2、字母、数字、下划线(不包含中文和其他符号)

QRegExp regx("[A-Za-z0-9_]{6,30}");
QValidator *validator = new QRegExpValidator(regx,ui->lineEdit_user);
ui->lineEdit_user->setValidator(validator);

3、字母、数字、字符(不包含中文)

QRegExp regx("^[^u4e00-u9fa5]{0,}$");
QValidator *validator = new QRegExpValidator(regx,ui->lineEdit_user);
ui->lineEdit_user->setValidator(validator);


如果以上提供的不够你的需求,请看这篇:http://www.52php.cn/article/p-vrwunmsv-zs.html

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读