限制用户输入 正则表达式 去除中文 中文符号

来源:互联网 发布:python 爬虫库推荐 编辑:程序博客网 时间:2024/06/04 20:13

最近一个判断需要判断字符串中的字符类型,因此想起了正则表达式,查了下相关文档,得到如下结果:

  1. 判断字符串中是否含中文

QT中使用此判断:

bool use_chinese = str.contains(QRegExp("[\\x4e00-\\x9fa5]+"));   if(use_chinese){       QMessageBox::warning(NULL, "注意", "字符串含中文字符!", QMessageBox::Ok );       return;   }

核心是[\\x4e00-\\x9fa]表明简体中文字符范围

关于字符范围的详细信息可参考:正则表达式的汉字匹配

  1. QlineEdit 的显示模式

四种显示模式:

QLineEdit *pNormalLineEdit = new QLineEdit(this);  QLineEdit *pNoEchoLineEdit = new QLineEdit(this);  QLineEdit *pPasswordLineEdit = new QLineEdit(this);  QLineEdit *pPasswordEchoOnEditLineEdit = new QLineEdit(this);  pNormalLineEdit->setPlaceholderText("Normal");  pNoEchoLineEdit->setPlaceholderText("NoEcho");  pPasswordLineEdit->setPlaceholderText("Password");  pPasswordEchoOnEditLineEdit->setPlaceholderText("PasswordEchoOnEdit");    // 设置显示效果  pNormalLineEdit->setEchoMode(QLineEdit::Normal);  pNoEchoLineEdit->setEchoMode(QLineEdit::NoEcho);  pPasswordLineEdit->setEchoMode(QLineEdit::Password);  pPasswordEchoOnEditLineEdit->setEchoMode(QLineEdit::PasswordEchoOnEdit);
  1. LineEdit验证器

验证器验证输入字符要求

QLineEdit *pIntLineEdit = new QLineEdit(this);  QLineEdit *pDoubleLineEdit = new QLineEdit(this);  QLineEdit *pValidatorLineEdit = new QLineEdit(this);    pIntLineEdit->setPlaceholderText(QString::fromLocal8Bit("整形"));  pDoubleLineEdit->setPlaceholderText(QString::fromLocal8Bit("浮点型"));  pValidatorLineEdit->setPlaceholderText(QString::fromLocal8Bit("字母和数字"));    // 整形 范围:[1, 99]  QIntValidator *pIntValidator = new QIntValidator(this);  pIntValidator->setRange(1, 99);    // 浮点型 范围:[-360, 360] 精度:小数点后2位  QDoubleValidator *pDoubleValidator = new QDoubleValidator(this);  pDoubleValidator->setRange(-360, 360);  pDoubleValidator->setNotation(QDoubleValidator::StandardNotation);  pDoubleValidator->setDecimals(2);    // 字符和数字  QRegExp reg("[a-zA-Z0-9]+$");//定义一个正则表达式模式  QRegExpValidator *pValidator = new QRegExpValidator(this);  pValidator->setRegExp(reg);//自定义一个校验器    pIntLineEdit->setValidator(pIntValidator);  pDoubleLineEdit->setValidator(pDoubleValidator);  pValidatorLineEdit->setValidator(pValidator);
  1. LineEdit掩码输入

ip,mac,日期,许可证

QLineEdit *pIPLineEdit = new QLineEdit(this);  QLineEdit *pMACLineEdit = new QLineEdit(this);  QLineEdit *pDateLineEdit = new QLineEdit(this);  QLineEdit *pLicenseLineEdit = new QLineEdit(this);    pIPLineEdit->setInputMask("000.000.000.000;_");  pMACLineEdit->setInputMask("HH:HH:HH:HH:HH:HH;_");  pDateLineEdit->setInputMask("0000-00-00");  pLicenseLineEdit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#");

2,3,4参考:Qt之QLineEdit

  1. 常用正则表达式和表达式符号简介
  • 不包含标点字符和特殊字符 QRegularExpression rx("^(?![\\p{P}\\p{S}])[\u4e00-\u9fa5]+$");
    参考:qt QRegularExpression 中文不包括“标点符号 特殊字符“ 的正则表达式
  • 匹配中文英文数字及"_"同时判断长度: [\u4e00-\u9fa5_a-zA-Z0-9_]{4,10}
    参考:正则表达式 匹配中文,英文字母和数字及_的写法!同时控制长度
  • QT官方文档(自己去看)
原创粉丝点击