add:添加正则最常用的基本介绍。

This commit is contained in:
taynpg 2025-06-13 11:22:35 +08:00
parent 5c2b4b211a
commit b30b81c1d9
4 changed files with 122 additions and 10 deletions

View File

@ -1,5 +1,6 @@
{
"files.associations": {
"qapplication": "cpp"
"qapplication": "cpp",
"xstring": "cpp"
}
}

View File

@ -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 |
| `?` | 01 |
| `{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('.');

View File

@ -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);

View File

@ -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>