Qt【正则表达式】匹配中文汉字,和字母,过滤中英文符号

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lilili1010101010/article/details/84028340
 QLineEdit *name_lineEdit = new QLineEdit;
QRegExp regx("^[\u4e00-\u9fa5a-zA-Z]+$");  //其中匹配中文[\u4e00-\u9fa5]
QValidator *validator = new QRegExpValidator(regx);
name_lineEdit->setValidator(validator);

上面这个正则表达式没有过滤掉中文符号,本想一个一个过滤,(如下),仍然无效

QRegExp regx("[^`~!@#$%^&*()-_+={}':;',\\[\\].<>/?¥……()——;’‘:“',。、?0-9]+$");
1
后响应QLineEdit 的textChanged的信号,在槽函数中过滤中文符号,成功!正则表达式无法过滤也因为编码问题。

connect(name_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotNameFilter(QString)));

void SearchCondWidget::slotNameFilter(QString s)   //SearchCondWidget我的类
{
    s.replace(QString::fromLocal8Bit("【"), "");
    s.replace(QString::fromLocal8Bit("】"), "");
    s.replace(QString::fromLocal8Bit("?"), "");
    s.replace(QString::fromLocal8Bit("!"), "");
    s.replace(QString::fromLocal8Bit("·"), "");
    s.replace(QString::fromLocal8Bit("¥"), "");
    s.replace(QString::fromLocal8Bit("……"), "");
    s.replace(QString::fromLocal8Bit("("), "");
    s.replace(QString::fromLocal8Bit(")"), "");
    s.replace(QString::fromLocal8Bit("——"), "");
    s.replace(QString::fromLocal8Bit("、"), "");
    s.replace(QString::fromLocal8Bit(":"), "");
    s.replace(QString::fromLocal8Bit(";"), "");
    s.replace(QString::fromLocal8Bit("“"), "");    //引号两边分开过滤
    s.replace(QString::fromLocal8Bit("”"), "");
    s.replace(QString::fromLocal8Bit("’"), "");    //引号两边分开过滤
    s.replace(QString::fromLocal8Bit("‘"), "");
    s.replace(QString::fromLocal8Bit("《"), "");
    s.replace(QString::fromLocal8Bit("》"), "");
    s.replace(QString::fromLocal8Bit(","), "");
    s.replace(QString::fromLocal8Bit("。"), "");
    name_lineEdit->setText(s);
}

其中这个双引号(“”)和单引号(‘’)两边要分开过滤
--------------------- 
版权声明:本文为CSDN博主「哒哒的九九」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lilili1010101010/article/details/84028340

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值