QT正则表达式提取指定条件字符串以及字符串条件的判断
QRegExp rx2;
rx2.setPatternSyntax(QRegExp::RegExp);
rx2.setCaseSensitivity(Qt::CaseSensitive);//大小写
rx2.setPattern(ui->textEdit_reg_condition->toPlainText());//textEdit_reg_condition判断条件编辑框
//------元字符---
//rx.setPattern(QString("^[A-Za-z0-9]+$")); //匹配所有大小写字母和数字组成的字符串,Aa1232--true,Aa1232@--false
//rx.setPattern(QString("a[5]")); //匹配a开头,后面是指定包含5的字符串,a5--true,a6--false
//rx.setPattern(QString("a[5678c]")); //匹配a开头,a5,a6,a7,a8,ac--true,ad--false
//rx.setPattern(QString("^cd.a2$")); //匹配cd开头,a2结尾的字符串,cd1a2--true,cd12a2--false,cd后面的.点,一个点代表一位(. 匹配任意单个字符)
//------转义字符---https://txwtech.blog.csdn.net/article/details/126408242?spm=1001.2014.3001.5502
//-----/w 匹配字母、数字、下划线、汉字
//rx.setPattern(QString("\bcd\w*a2\b")); //匹配以cd开头,a2结尾的任意字符串,cd1a2--true,cd123a2--true,\w后面的*, 匹配任意字符
//rx.setPattern(QString("\w{2}"));//字符重复的次数,cc--true,ccc--false
//rx.setPattern(QString("\w{1,3}"));//字符重复表达式至少重复1次,最多重复3次,c,cc或者ccc--true,ccccc--false
//rx.setPattern(QString("b2\w{1,3}"));//字符重复表达式至少重复1次,最多重复3次,b2c,b2cc或者b2ccc--true,b2ccccc--false
//rx.setPattern(QString("b2\w*\d+")); //b2开头,\w*中间任意字符+至少一个数字结尾的条件。b2cc5--true,
//rx.setPattern(QString("b2\w*\d*")); //b2开头,中间任意字符+结尾。b2cc5,或者b2cc--true,d*表示没有数字结尾也符合条件
//rx.setPattern(QString("b2\w*\d{3,}")); //b2开头,中间任意字符+结尾。b2cc523--true,d{3,}表示至少有3个数字
//rx.setPattern(QString("\w*\d{3,}")); //txw523、_132、cc1232 --true,d{3,}表示至少有3个数字
//rx.setPattern(QString("@\d{3,}")); //@1232 --true,d{3,}表示至少有3个数字
//rx.setPattern(QString("\w*[^abc]")); //字符串:ddffed 不包含abc --true,字符串ttabcdefg包含abc--false
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRegExp>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//background-image: url(:/button_bg.png);
QString pic_path=tr(":/button_bg.png");
QString pic_path_hover=tr(":/button_pause_icon.png");
QString styleSheet=QString("QPushButton{border-image:url(%1);background-color:transparent;border:none;}"
"QPushButton:hover{border-image:url(%2);background-color:transparent;font: 12pt \"楷体\";}"
"QPushButton:pressed{border-image:none;background-color: rgb(0, 170, 0);font: 18pt \"楷体\";}"
).arg(pic_path).arg(pic_path_hover);
ui->pushButton_reg_validate->setStyleSheet(styleSheet);
ui->textEdit_reg_condition->setText("b[5]");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_reg_validate_clicked()
{
QString reg_str=ui->textEdit_reg_condition->toPlainText();
QRegExp exp2(reg_str);
bool b_Valid=exp2.isValid();
if(b_Valid==true)
{
ui->label_show_msg->setText("Reg格式正确");
}
else
{
ui->label_show_msg->setText("Reg格式错误");
}
}
void MainWindow::on_pushButton_start_to_match_clicked()
{
QRegExp rx2;
rx2.setPatternSyntax(QRegExp::RegExp);
rx2.setCaseSensitivity(Qt::CaseSensitive);//大小写
rx2.setPattern(ui->textEdit_reg_condition->toPlainText());//textEdit_reg_condition判断条件编辑框
//------元字符---
//rx.setPattern(QString("^[A-Za-z0-9]+$")); //匹配所有大小写字母和数字组成的字符串,Aa1232--true,Aa1232@--false
//rx.setPattern(QString("a[5]")); //匹配a开头,后面是指定包含5的字符串,a5--true,a6--false
//rx.setPattern(QString("a[5678c]")); //匹配a开头,a5,a6,a7,a8,ac--true,ad--false
//rx.setPattern(QString("^cd.a2$")); //匹配cd开头,a2结尾的字符串,cd1a2--true,cd12a2--false,cd后面的.点,一个点代表一位(. 匹配任意单个字符)
//------转义字符---https://txwtech.blog.csdn.net/article/details/126408242?spm=1001.2014.3001.5502
//-----/w 匹配字母、数字、下划线、汉字
//rx.setPattern(QString("\bcd\w*a2\b")); //匹配以cd开头,a2结尾的任意字符串,cd1a2--true,cd123a2--true,\w后面的*, 匹配任意字符
//rx.setPattern(QString("\w{2}"));//字符重复的次数,cc--true,ccc--false
//rx.setPattern(QString("\w{1,3}"));//字符重复表达式至少重复1次,最多重复3次,c,cc或者ccc--true,ccccc--false
//rx.setPattern(QString("b2\w{1,3}"));//字符重复表达式至少重复1次,最多重复3次,b2c,b2cc或者b2ccc--true,b2ccccc--false
//rx.setPattern(QString("b2\w*\d+")); //b2开头,\w*中间任意字符+至少一个数字结尾的条件。b2cc5--true,
//rx.setPattern(QString("b2\w*\d*")); //b2开头,中间任意字符+结尾。b2cc5,或者b2cc--true,d*表示没有数字结尾也符合条件
//rx.setPattern(QString("b2\w*\d{3,}")); //b2开头,中间任意字符+结尾。b2cc523--true,d{3,}表示至少有3个数字
//rx.setPattern(QString("\w*\d{3,}")); //txw523、_132、cc1232 --true,d{3,}表示至少有3个数字
//rx.setPattern(QString("@\d{3,}")); //@1232 --true,d{3,}表示至少有3个数字
//rx.setPattern(QString("\w*[^abc]")); //字符串:ddffed 不包含abc --true,字符串ttabcdefg包含abc--false
QString raw_str=ui->textEdit_input_str->toPlainText();//textEdit_input_str字符串编辑框
bool b_result=rx2.exactMatch(raw_str); //exactMatch(QString),返回是否整串匹配,匹配一部分也返回false。
if(b_result==true)
{
ui->label_show_msg->setText("符合条件");
}
else
{
ui->label_show_msg->setText("不符合条件");
}
}
void MainWindow::on_pushButton_pick_string_clicked()//字符信息提取
{
QRegExp rx2;
rx2.setPatternSyntax(QRegExp::RegExp);
rx2.setCaseSensitivity(Qt::CaseSensitive);//大小写
rx2.setPattern(ui->textEdit_reg_condition->toPlainText());//textEdit_reg_condition判断条件编辑框
QString raw_str=ui->textEdit_input_str->toPlainText();//textEdit_input_str字符串编辑框
QStringList list_str;
int pos2=0;
while((pos2=rx2.indexIn(raw_str,pos2))!=-1)
{
list_str<<rx2.cap(1);
pos2+=rx2.matchedLength();
}
QString str_msg;
foreach(QString str,list_str)
{
str_msg=str_msg+str+" ";
}
ui->label_show_msg->setText(str_msg);
qDebug()<<list_str;
//-------------------按规则提取字符串----------------
QRegExp rx5;
rx5.setPatternSyntax(QRegExp::RegExp);
rx5.setCaseSensitivity(Qt::CaseSensitive);//大小写
rx5.setPattern(QString("(SLA.*\\$)"));//提取SLA开始,$结尾的字符串,. (一个小点)匹配任意单个字符.例如, 2.8 可能是2. 后面跟任意字符,再跟8,匹配除了换行符以外的任意字符
//https://txwtech.blog.csdn.net/article/details/126408242?spm=1001.2014.3001.5502
// rx5.setPattern(QString("(\\d+)"));//提取数字
QString raw_str5="this is good SLA which is 20 years old $ nice day";//textEdit_input_str字符串编辑框
QStringList list_str5;
int pos5=0;
while((pos5=rx5.indexIn(raw_str5,pos5))!=-1)
{
list_str5<<rx5.cap(1);
pos5+=rx5.matchedLength();
}
QString str_msg5;
foreach(QString str,list_str5)
{
str_msg5=str_msg5+str;
}
//ui->label_show_msg->setText(str_msg);
qDebug()<<"str_msg5:"<<str_msg5;
}