Boost之正则表达式

很长时间没写过博客了,重新捡起来,正好最近在使用Boost做项目,就先写一些Boost相关的知识吧。

写了一个简单的QtGUI Demo来测试boost正则表达式,主程序如下:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
 
void MainWindow::on_match_clicked()
{
    QLineEdit * srcEdit = this->findChild<QLineEdit *>("srcEdit");
    QLineEdit * matchEdit = this->findChild<QLineEdit *>("matchEdit");
 
    std::string src_data = srcEdit->text().toUtf8().data();        //源文本
    std::string match_data = matchEdit->text().toUtf8().data();    //匹配模式
    boost::regex regex(match_data);
 
    if(boost::regex_match(src_data, regex))
    {
        qDebug()<<"Match"<<endl;
    }
    else
    {
        qDebug()<<"Un-Match"<<endl;
    }
 
}
 
使用上面代码,输入如下参数:
srcEdit = "test", 
matchEdit = "te"
输出 Un-Match
 
输入:
srcEdit = "test"
matchEdit = "te.*"
输出Match
 
所以上述正则匹配的方式必须是正则表达式完全匹配文本才行。
 
下面再加上查找子串的代码,代码如下:
void MainWindow::on_SubPartten_clicked()
{
    QLineEdit * srcEdit = this->findChild<QLineEdit *>("srcEdit");
    QLineEdit * matchEdit = this->findChild<QLineEdit *>("matchEdit");
    std::string src_data = srcEdit->text().toUtf8().data();
    std::string match_data = matchEdit->text().toUtf8().data();
    boost::regex regex(match_data);
    boost::match_results<std::string::const_iterator> what;
    if(0 == boost::regex_match(src_data, what, regex, boost::match_default))
    {
        qDebug()<<"Un-Match"<<endl;
    }
    else
    {
        qDebug()<<"Match"<<endl;
        for (unsigned int i = 1; i < what.size(); i++)
        {
            qDebug()<<i<<" "<<what[i].str().c_str()<<endl;
        }
    }
}
输入:
srcEdit= "2013/05/13";
matchEdit = "(\d+)/(\d+)/(\d+)"; //注意,我是在QEdit中编辑的,如果是在代码中写,需要改为(\\d+)/(\\d+)/(\\d+)
输出如下:

Match

1 2013

2 05

3 13 


 
 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中,"vs"通常指的是"Visual",它是一个由微软开发的集成开发环境(IDE)。正则表达式是一种用于匹配和操作文本的强大工具。在C++中,你可以使用正则表达式库来处理字符串数据,并进行模式匹配、替换等操作。 C++标准库中并没有原生支持正则表达式,但你可以使用第三方库来处理正则表达式。一些常用的C++正则表达式库包括Boost.Regex、PCRE(Perl Compatible Regular Expressions)和RE2。 使用正则表达式需要先包含相应的头文件,然后使用库提供的函数或类来进行匹配和操作。你可以在代码中定义模式,并使用正则表达式函数来进行匹配、查找、替换等操作。 以下是一个简单示例,展示了如何在C++中使用Boost.Regex库进行正则表达式匹配: ```cpp #include <iostream> #include <boost/regex.hpp> int main() { std::string text = "Hello, world!"; boost::regex pattern("world"); if (boost::regex_search(text, pattern)) { std::cout << "Pattern found in the text!" << std::endl; } else { std::cout << "Pattern not found in the text." << std::endl; } return 0; } ``` 上述代码中,我们使用了Boost.Regex库来定义一个名为"pattern"的正则表达式,并使用boost::regex_search函数在字符串"text"中进行匹配。如果找到了匹配的模式,就输出"Pattern found in the text!",否则输出"Pattern not found in the text."。 请注意,具体的正则表达式语法和用法可以参考相关文档和教程,以便更深入地了解如何在C++中使用正则表达式

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值