httplb 服务器

#include <iostream>
#include "httplib.h"    
using namespace std;
using namespace httplib;
int main(){
    Server svr;
    svr.Get("/",[](const Request& req,Response& res){
        res.set_content("Hello World", "text/plain");
    });
    svr.Get("/gret/(.*)",[](const Request& req,Response& res){
        auto name=req.matches[1];
        res.set_content(name,"text/plain");
    }); 
    svr.Get("/json", [](const Request& req, Response& res) {
        res.set_content(R"({"status": "ok"})", "application/json");
    });
    cout<<"Server running at http:127.0.0.1\n";
    svr.listen("0.0.0.0", 80);
    
}

 正则表达式(Pegular Expression)是一种用于字符串匹配和搜索的强大工具,它通过特定符号组合来定义为文本模式。

一下是核心符号及其含义:

一 、基础符号

1.^ 和 $

  ^匹配字符串开头,$匹配字符串结尾。

  示例:^A匹配以字母A开头的行,t$匹配以字母t结尾的行。

2.  。

  匹配任意单个字符(除换行符外)。

  示例:a.c可匹配 abc 或 a2c。

3. * + ?

   *匹配前一个字符0次或多次 +匹配1次或多次,?匹配0次或1次。

  示例:a*匹配空字符串,a 或 aaa。

二、字符类与转义

1.\d,  \w,  \s

  \d 匹配数字(等价于[0-9]),\w匹配字母/数字/下划线,\s 匹配空白字符。

  示例:\的{3} 匹配 123.

2.[ ]  和  [^]

  [abc] 匹配a、b或c,[^abc]匹配非a、b、c的字符。

  示例:[A-Z]匹配任意大写字母

 三、量词与分组

1.{ n } 、{ n, } 、{ n,m }

  分别表示精确匹配n次、至少n次、n到m次。

  示例:a{ 2,4 } 匹配 aa 、aaa 或 aaaa 。

2.( )

  用于分组和捕获匹配内容。

·  实例:(foo)可提取 foo 作为字串。

四、高级用法

1、|

  表示 “或” 关系,匹配多个模式之一。

  示例:green | red 匹配 green 或 red 。

2.(?=. . .)和(?!. . .)

  正向/负向先行断言,用于匹配位置而非字符。

  示例:er(? = gular ) 匹配 regular 中的 re 。

示例代码#include <bits/stdc++.h>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
    while(1){
        string name;
        cin>>name;
//        regex std(R"(^[0-9]+$)");
//        regex std(R"(^[A-Za-z]+$)");
//         regex std(R"(^\d{6}+$)");
//        regex std(R"(.*[A-Z]+.*)");    
//        regex std(R"(^[0-9,a-z,A-z]+@\w+\.[a-zA-Z]+&\.?)");    
//        regex std(R"(^1[356789][0-9]{9}$)");
//     regex b(R"(^((25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$)");
cout<<boolalpha<<regex_search(name,std); } return 0; }

 网址数据提取

#include <iostream>
#include <bits/stdc++.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
        string name="https://chat.baidu.com/search?word=123";    
        regex std(R"((^[A-Za-z]+)://([A-Za-z\.]+)/(\w+)\?(\w+)=(\d+))");
        smatch exe;
        if(regex_search(name,exe,std)){    
            cout<<exe[0]<<endl<<exe[1]<<endl<<exe[2]<<endl<<exe[3]<<endl<<exe[5]<<endl;    
        }
    return 0;
}

 

posted @ 2025-09-21 09:57  爱吃泡面的皮卡  阅读(7)  评论(0)    收藏  举报