1.对输入框中用户输入内容的限制,比如只能输入数字,并且最多5位数(如果是int类型,输入位数过多会发生溢出产生问题);
2.检查输入是否正确;
3.获取一个字符串中的一段内容,比如获取2015-11-20中的2015。
. | 除换行符以外的任意字符 |
* | 重复0次或多次 |
+ | 重复1次或多次 |
? | 重复0次或1次 |
{m,n} | 重复m到n次 |
\d | 数字 |
\s | 空白符 |
说明 | 正则表达式 |
---|---|
网址(URL) | [a-zA-z]+://[^\s]* |
IP地址(IP Address) | ((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?) |
电子邮件(Email) | \w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)* |
QQ号码 | [1-9]\d{4,} |
HTML标记(包含内容或自闭合) | <(.*)(.*)>.*<\/\1>|<(.*) \/> |
密码(由数字/大写字母/小写字母/标点符号组成,四种都必有,8位以上) | (?=^.{8,}$)(?=.*\d)(?=.*\W+)(?=.*[A-Z])(?=.*[a-z])(?!.*\n).*$ |
日期(年-月-日) | (\d{4}|\d{2})-((1[0-2])|(0?[1-9]))-(([12][0-9])|(3[01])|(0?[1-9])) |
日期(月/日/年) | ((1[0-2])|(0?[1-9]))/(([12][0-9])|(3[01])|(0?[1-9]))/(\d{4}|\d{2}) |
时间(小时:分钟, 24小时制) | ((1|0?)[0-9]|2[0-3]):([0-5][0-9]) |
汉字(字符) | [\u4e00-\u9fa5] |
中文及全角标点符号(字符) | [\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee] |
中国大陆固定电话号码 | (\d{4}-|\d{3}-)?(\d{8}|\d{7}) |
中国大陆手机号码 | 1\d{10} |
中国大陆邮政编码 | [1-9]\d{5} |
中国大陆身份证号(15位或18位) | \d{15}(\d\d[0-9xX])? |
非负整数(正整数或零) | \d+ |
正整数 | [0-9]*[1-9][0-9]* |
负整数 | -[0-9]*[1-9][0-9]* |
整数 | -?\d+ |
小数 | (-?\d+)(\.\d+)? |
不包含abc的单词 | \b((?!abc)\w)+\b |
QT中使用正则表达式,首先要#include <QRegExp>
QRegExp regExp("0|[1-9][0-9]{1,4}"); ui.lineEdit->setValidator(new QRegExpValidator(regExp, this));
validator意思是“验证器”。
下面是QT助手中的相关解释——
Sets this line edit to only accept input that the validator, v, will accept. This allows you to place any arbitrary constraints on the text which may be entered.
If v == 0, setValidator() removes the current input validator. The initial setting is to have no input validator (i.e. any input is accepted up to maxLength()).
QRegExpValidator uses a regular expression (regexp) to determine whether an input string is Acceptable, Intermediate, or Invalid. The regexp can either be supplied when the QRegExpValidator is constructed, or at a later time.
When QRegExpValidator determines whether a string is Acceptable or not, the regexp is treated as if it begins with the start of string assertion (^) and ends with the end of string assertion ($); the match is against the entire input string, or from the given position if a start position greater than zero is given.
If a string is a prefix of an Acceptable string, it is considered Intermediate. For example, "" and "A" are Intermediate for the regexp [A-Z][0-9] (whereas "_" would be Invalid).
For a brief introduction to Qt‘s regexp engine, see QRegExp.
Example of use:
// regexp: optional ‘-‘ followed by between 1 and 3 digits QRegExp rx("-?\\d{1,3}"); QValidator *validator = new QRegExpValidator(rx, this); QLineEdit *edit = new QLineEdit(this); edit->setValidator(validator);
Below we present some examples of validators. In practice they would normally be associated with a widget as in the example above.
// integers 1 to 9999 QRegExp rx("[1-9]\\d{0,3}"); // the validator treats the regexp as "^[1-9]\\d{0,3}$" QRegExpValidator v(rx, 0); QString s; int pos = 0; s = "0"; v.validate(s, pos); // returns Invalid s = "12345"; v.validate(s, pos); // returns Invalid s = "1"; v.validate(s, pos); // returns Acceptable rx.setPattern("\\S+"); // one or more non-whitespace characters v.setRegExp(rx); s = "myfile.txt"; v.validate(s, pos); // Returns Acceptable s = "my file.txt"; v.validate(s, pos); // Returns Invalid // A, B or C followed by exactly five digits followed by W, X, Y or Z rx.setPattern("[A-C]\\d{5}[W-Z]"); v.setRegExp(rx); s = "a12345Z"; v.validate(s, pos); // Returns Invalid s = "A12345Z"; v.validate(s, pos); // Returns Acceptable s = "B12"; v.validate(s, pos); // Returns Intermediate // match most ‘readme‘ files rx.setPattern("read\\S?me(\.(txt|asc|1st))?"); rx.setCaseSensitive(false); v.setRegExp(rx); s = "readme"; v.validate(s, pos); // Returns Acceptable s = "README.1ST"; v.validate(s, pos); // Returns Acceptable s = "read me.txt"; v.validate(s, pos); // Returns Invalid s = "readm"; v.validate(s, pos); // Returns Intermediate
(2)检查输入是否符合格式
QRegExp rx("[1-9]\\d{0,3}"); QRegExpValidator v(rx, 0); int pos = 0; QString strInput = ui.lineEdit->text(); if (v.validate(strInput, pos)) { qDebug() << "yes"; } else { qDebug() << "no"; }
QT助手中的相关解释——
Reimplemented from QValidator::validate().
Returns Acceptable if input is matched by the regular expression for this validator, Intermediate if it has matched partially (i.e. could be a valid match if additional valid characters are added), and Invalid if input is not matched.
Additionally, if input is not matched, the pos parameter is set to the length of the input parameter.
For example, if the regular expression is \w\d\d (word-character, digit, digit) then "A57" is Acceptable, "E5" is Intermediate, and "+9" is Invalid.
This enum type defines the states in which a validated string can exist.
Constant Value Description
QValidator::Invalid 0 The string is clearly invalid.
QValidator::Intermediate 1 The string is a plausible intermediate value.
QValidator::Acceptable 2 The string is acceptable as a final result; i.e. it is valid.
原文:http://www.cnblogs.com/hellovenus/p/4984751.html