`
hgfghwq11
  • 浏览: 47475 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

QT正则表达式小例子

阅读更多

  判断合法IP的QT正则表达式:
  bool IsIPaddress(QString ip)
  {
  QRegExp rx2("(//d+)(//.)(//d+)(//.)(//d+)(//.)(//d +)");
  int pos = rx2.indexIn(ip);
  if(pos>-1)
  {
  for(int i=0;i=255 )
  {
  QMessageBox::information(this, tr("错误"), tr("IP地址错误"));
  return false;
  }
  } 
  if(rx2.cap(7).toInt()==0)
  {            
  QMessageBox::information(this, tr("错误"), tr("IP地址错误"));
  return false;
  }
  if(rx2.cap(7).toInt()==0)
  {
  QMessageBox::information(this, tr("错误"), tr("IP地址错误"));
  return false;
  }
  }
  else
  {
  QMessageBox::information(this, tr("错误"), tr("IP地址错误"));
  return false;
  }
  return true;
  }
  判断IP地址更简单的方式是:
  QRegExp rx2("^([1]?/d/d?|2[0-4]/d|25[0-5])/.([1]?/d/d?|2[0 -4]/d|25[0-5])/.([1]?/d/d?|2[0-4]/d|25[0-5])/.([1]? /d/d?|2[0-4]/d|25[0-5])$")
  if( !rx2.exactMatch(ip) )
  {
  QMessageBox::information(this, tr("错误"), tr("ip地址错误"));
  return false;
  }
  return true;
  判断文件名是否含有字母、数字、下划线之外的字符:
  bool IsRightFilename(QString filename)
  {
  QRegExp rx("[A-Za-z_0-9]+");
  if( !rx.exactMatch( filename) )
  {
  QMessageBox::information(this, tr("错误"), tr("文件名含有其他字符或中文字符"));
  return false;
  }
  return true;
  }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics