add:添加正则最常用的基本介绍。
This commit is contained in:
parent
5c2b4b211a
commit
b30b81c1d9
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -1,5 +1,6 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"qapplication": "cpp"
|
||||
"qapplication": "cpp",
|
||||
"xstring": "cpp"
|
||||
}
|
||||
}
|
119
MainWidget.cpp
119
MainWidget.cpp
@ -6,6 +6,7 @@
|
||||
#include <QRegularExpression>
|
||||
#include <QScreen>
|
||||
#include <QSettings>
|
||||
#include <QTextBrowser>
|
||||
#include <filesystem>
|
||||
|
||||
#include "./ui_MainWidget.h"
|
||||
@ -134,6 +135,8 @@ MainWidget::MainWidget(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi
|
||||
push_content(current_);
|
||||
});
|
||||
|
||||
connect(ui->btnRegIntro, &QPushButton::clicked, this, [&]() { showRegexIntro(); });
|
||||
|
||||
QSettings settings;
|
||||
settings.beginGroup("xmlopr");
|
||||
restoreGeometry(settings.value("geometry").toByteArray());
|
||||
@ -430,8 +433,20 @@ void MainWidget::search(const QString& key)
|
||||
return;
|
||||
}
|
||||
|
||||
QRegularExpression regex;
|
||||
if (!ui->cbCaseSensitive->isChecked()) {
|
||||
bkey = bkey.toUpper();
|
||||
regex = QRegularExpression(bkey);
|
||||
} else {
|
||||
regex = QRegularExpression(bkey, QRegularExpression::CaseInsensitiveOption);
|
||||
}
|
||||
|
||||
bool useRegex = false;
|
||||
if (!regex.isValid()) {
|
||||
if (!ui->cbCaseSensitive->isChecked()) {
|
||||
bkey = bkey.toUpper();
|
||||
}
|
||||
} else {
|
||||
useRegex = true;
|
||||
}
|
||||
|
||||
current_.clear();
|
||||
@ -439,14 +454,24 @@ void MainWidget::search(const QString& key)
|
||||
for (auto i = 0; i < keys_.size(); ++i) {
|
||||
const char* data = item->Attribute(keys_[i].c_str());
|
||||
QString qdata(data);
|
||||
if (!ui->cbCaseSensitive->isChecked()) {
|
||||
qdata = qdata.toUpper();
|
||||
|
||||
if (useRegex) {
|
||||
QRegularExpressionMatch match = regex.match(qdata);
|
||||
if (!match.hasMatch()) {
|
||||
continue;
|
||||
}
|
||||
current_.push_back(item);
|
||||
break;
|
||||
} else {
|
||||
if (!ui->cbCaseSensitive->isChecked()) {
|
||||
qdata = qdata.toUpper();
|
||||
}
|
||||
if (!qdata.contains(bkey)) {
|
||||
continue;
|
||||
}
|
||||
current_.push_back(item);
|
||||
break;
|
||||
}
|
||||
if (!qdata.contains(bkey)) {
|
||||
continue;
|
||||
}
|
||||
current_.push_back(item);
|
||||
break;
|
||||
}
|
||||
}
|
||||
push_content(current_, ui->edCurPage->text().toUInt(), true);
|
||||
@ -527,6 +552,12 @@ bool MainWidget::edit_property(Element_t* target, int row, bool is_copy)
|
||||
Property_t property;
|
||||
xml_.get_attributes(target, property);
|
||||
|
||||
if (is_copy) {
|
||||
attri_edit_->setWindowTitle(u8"复制项目");
|
||||
} else {
|
||||
attri_edit_->setWindowTitle(u8"编辑项目");
|
||||
}
|
||||
|
||||
// 检测key值是否变化
|
||||
std::string value_pre = property[0].value;
|
||||
attri_edit_->set_attribute(property);
|
||||
@ -1009,6 +1040,78 @@ void MainWidget::clear_tab_widget()
|
||||
}
|
||||
}
|
||||
|
||||
void MainWidget::showRegexIntro() // btnRegIntro
|
||||
{
|
||||
// 创建一个对话框窗口
|
||||
QDialog* dialog = new QDialog(this);
|
||||
dialog->setWindowTitle("QRegularExpression 基本语法");
|
||||
dialog->resize(800, 600);
|
||||
|
||||
// 创建布局
|
||||
QVBoxLayout* layout = new QVBoxLayout(dialog);
|
||||
|
||||
// 创建文本浏览器用于显示Markdown内容
|
||||
QTextBrowser* textBrowser = new QTextBrowser(dialog);
|
||||
textBrowser->setOpenExternalLinks(true);
|
||||
|
||||
// 设置Markdown内容
|
||||
QString markdownContent = R"(
|
||||
# QRegularExpression 基本语法(简化版)
|
||||
|
||||
## 1. 常用元字符
|
||||
|
||||
| 语法 | 说明 |
|
||||
|------|------|
|
||||
| `.` | 匹配任意单个字符(除换行符) |
|
||||
| `\d` | 匹配数字(0-9) |
|
||||
| `\D` | 匹配非数字 |
|
||||
| `\w` | 匹配字母、数字或下划线([A-Za-z0-9_]) |
|
||||
| `\W` | 匹配非单词字符 |
|
||||
| `\s` | 匹配空白字符(空格、制表符等) |
|
||||
| `\S` | 匹配非空白字符 |
|
||||
| `[...]` | 匹配括号内的任意字符(如[aeiou]) |
|
||||
| `[^...]` | 匹配不在括号内的任意字符 |
|
||||
|
||||
## 2. 量词
|
||||
|
||||
| 语法 | 说明 |
|
||||
|------|------|
|
||||
| `*` | 匹配0次或多次 |
|
||||
| `+` | 匹配1次或多次 |
|
||||
| `?` | 匹配0次或1次 |
|
||||
| `{n}` | 匹配恰好n次 |
|
||||
| `{n,}` | 匹配至少n次 |
|
||||
| `{n,m}` | 匹配n到m次 |
|
||||
|
||||
## 3. 锚点
|
||||
|
||||
| 语法 | 说明 |
|
||||
|------|------|
|
||||
| `^` | 匹配字符串开头 |
|
||||
| `$` | 匹配字符串结尾 |
|
||||
| `\b` | 匹配单词边界 |
|
||||
|
||||
## 4. 转义字符
|
||||
|
||||
| 语法 | 说明 |
|
||||
|------|------|
|
||||
| `\\` | 匹配反斜杠 |
|
||||
| `\.` | 匹配点号 |
|
||||
| `\*` | 匹配星号 |
|
||||
|
||||
## 示例代码
|
||||
|
||||
```cpp
|
||||
QRegularExpression re("^\\d{3}-\\d{2}-\\d{4}$"); // 匹配SSN格式
|
||||
if (re.match("123-45-6789").hasMatch()) {
|
||||
qDebug() << "Valid SSN";
|
||||
}
|
||||
)";
|
||||
textBrowser->setMarkdown(markdownContent);
|
||||
layout->addWidget(textBrowser);
|
||||
dialog->exec();
|
||||
}
|
||||
|
||||
std::string MainWidget::extract_prefix(const std::string& name)
|
||||
{
|
||||
auto pos = name.find('.');
|
||||
|
@ -80,6 +80,7 @@ private:
|
||||
void unit_change();
|
||||
bool format_xml();
|
||||
void clear_tab_widget();
|
||||
void showRegexIntro();
|
||||
|
||||
private:
|
||||
std::string extract_prefix(const std::string& name);
|
||||
|
@ -246,6 +246,13 @@
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="btnRegIntro">
|
||||
<property name="text">
|
||||
<string>正则简介</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
@ -336,7 +343,7 @@
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1171</width>
|
||||
<height>33</height>
|
||||
<height>25</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
|
Loading…
x
Reference in New Issue
Block a user