当前位置: 首页 > news >正文

哈尔滨网站建设设计公司seo sem推广

哈尔滨网站建设设计公司,seo sem推广,厦门网站建设 孚珀科技,淘宝客网站开发定制文章目录 前言一、什么是正则表达式?二、正则表达式在动态路由中的作用三、实现一个简单的路由调度器总结 前言 动态路由有很多种实现方式,支持的规则、性能等有很大的差异。例如开源的路由实现gorouter支持在路由规则中嵌入正则表达式,例如…

文章目录

  • 前言
  • 一、什么是正则表达式?
  • 二、正则表达式在动态路由中的作用
  • 三、实现一个简单的路由调度器
  • 总结


前言

动态路由有很多种实现方式,支持的规则、性能等有很大的差异。例如开源的路由实现gorouter支持在路由规则中嵌入正则表达式,例如/p/[0-9A-Za-z]+,即路径中的参数仅匹配数字和字母。
最近在看cpp-httplib源码的时候,发现这个开源库是也是利用正则化来实现动态路由的。本文就来总结httplib是如何利用正则表达式来进行动态路由的,

一、什么是正则表达式?

正则表达式是一种用于描述字符串模式的工具。它由一系列字符和特殊字符组成,可以用来匹配、搜索、替换和提取字符串中的特定模式。正则表达式提供了一种灵活而强大的方式来处理各种字符串操作。
正则表达式的语法和例子
正则表达式的语法由普通字符和特殊字符组成。普通字符表示它们自身,而特殊字符具有特殊的含义和功能。
正则表达式的特殊字符包括:
.:匹配任意字符(除了换行符)。
*:匹配前面的元素零次或多次。
+:匹配前面的元素一次或多次。
?:匹配前面的元素零次或一次。
[]:定义字符集,匹配其中的任意一个字符。
():捕获分组,用于提取匹配结果。
\:转义字符,用于匹配特殊字符本身。

二、正则表达式在动态路由中的作用

动态路由是一种常见的路由处理方式,它允许根据请求的路径来选择相应的处理函数。正则表达式在动态路由中的作用是用于匹配和提取请求路径中的参数。
在动态路由中,我们可以使用正则表达式来定义路径模式,以便根据请求的路径来选择相应的处理函数加粗样式。例如,我们可以使用正则表达式来匹配带有数字参数的路径(如/products/123),或者匹配带有字母参数的路径(如/products/abc)。
通过使用正则表达式,我们可以灵活地匹配各种不同的路径格式,并提取匹配的参数。在路由分发器中,我们可以使用正则表达式的regex_match函数来尝试匹配请求路径和每个路由处理程序的路径模式。如果匹配成功,我们可以访问和使用匹配的结果,例如提取数字参数或字母参数。
通过正则表达式的匹配和提取功能,我们可以根据不同的请求路径选择相应的处理函数,并使用提取的参数执行相应的逻辑操作。这使得动态路由更加灵活和强大,能够处理各种不同的请求路径和参数。

三、实现一个简单的路由调度器

使用正则表达式来匹配请求的 URL,并根据匹配结果调用相应的处理程序。通过使用正则表达式,我们可以灵活地定义 URL 模式,并根据请求的 URL 动态地匹配和调度处理程序。
首先,我们定义了 Handler 类型,它是一个接受两个字符串参数的函数对象。然后,我们定义了 Handlers 类型,它是一个存储了正则表达式和处理程序的向量。

接下来,我们有一个 Post 函数,它接受一个 URL 模式和一个处理程序,并将它们作为键值对存储在 post_handlers_ 向量中。在这里,我们使用 std::regex 来将 URL 模式编译为正则表达式,并将其与处理程序一起存储。

最后,我们有一个 dispatch_request 函数,它接受请求字符串、响应字符串和处理程序的向量作为参数。该函数遍历处理程序向量,对每个处理程序执行以下操作:
1.获取存储在键值对中的正则表达式和处理程序。
2.使用std:regex_match函数来检查请求字符串是否与正则表达式匹
配。如果匹配成功,将匹配结果存储在std::smatch对象m中。
3.执行处理程序,并将请求字符串和响应字符串作为参数传递给它。
4.返回true表示成功匹配并调用了处理程序。
如果没有找到匹配的处理程序,函数将返回 false,表示未能处理该请求。


using Handler = std::function<void(const string &, string &)>;
using Handlers = std::vector<std::pair<std::regex, Handler>>;
Handlers post_handlers_;
void Post(const std::string &pattern, Handler handler) {post_handlers_.push_back(std::make_pair(std::regex(pattern), std::move(handler)));return ;
}inline bool dispatch_request(string &req, string &res,const Handlers &handlers) {for (const auto &x : handlers) {const auto &pattern = x.first;const auto &handler = x.second;std::smatch m;if (regex_match(req, m, pattern)) {// pattern  如果匹配 req  那么就回吧结果匹配到m中 cout<<"/match"<<endl;for (const auto& match : m) {std::cout << match << " ";}cout<<endl;cout<<"/"<<endl;handler(req, res);return true;}}return false;
}

使用如下:

   Post("/", homeHandler);Post("/products/(\\d+)/asd", productHandler);Post("/products/(\\w+)/asd$", contactHandler);string request = "/products/123/asd";string response;// 调度请求并处理if (dispatch_request(request, response, post_handlers_)) {cout << "Response: " << endl;cout <<  response << endl;} else {cout << "No matching route found." << endl;}

总结

通过使用正则表达式,我们可以定义灵活的路径模式,并根据请求的路径选择相应的处理函数。正则表达式还可以提取匹配的参数,使得动态路由能够处理各种不同的请求路径和参数。正则表达式的强大功能为动态路由的实现提供了便利和灵活性。


文章转载自:
http://dinncodethrone.stkw.cn
http://dinncoshortish.stkw.cn
http://dinncoshrovetide.stkw.cn
http://dinncocopyread.stkw.cn
http://dinncoreinstitution.stkw.cn
http://dinncosteadfast.stkw.cn
http://dinncoroncador.stkw.cn
http://dinncorheophil.stkw.cn
http://dinncowigwam.stkw.cn
http://dinncoeuryphage.stkw.cn
http://dinncomodernistic.stkw.cn
http://dinncovela.stkw.cn
http://dinncoparaceisian.stkw.cn
http://dinncodeejay.stkw.cn
http://dinncounbe.stkw.cn
http://dinncodependence.stkw.cn
http://dinncosignatary.stkw.cn
http://dinncopursily.stkw.cn
http://dinncorsc.stkw.cn
http://dinncoseptemvir.stkw.cn
http://dinncoscrew.stkw.cn
http://dinncophagocyte.stkw.cn
http://dinncostaphylinid.stkw.cn
http://dinncopluviose.stkw.cn
http://dinncocivism.stkw.cn
http://dinncoweldless.stkw.cn
http://dinncogranddam.stkw.cn
http://dinncocouturiere.stkw.cn
http://dinncozoonosis.stkw.cn
http://dinncoabatement.stkw.cn
http://dinncowallpiece.stkw.cn
http://dinncosquail.stkw.cn
http://dinncotorpidness.stkw.cn
http://dinncocardiogram.stkw.cn
http://dinncosetline.stkw.cn
http://dinnconobiliary.stkw.cn
http://dinncosuppleness.stkw.cn
http://dinncoflogging.stkw.cn
http://dinncointransitivize.stkw.cn
http://dinncostarvation.stkw.cn
http://dinncothuggism.stkw.cn
http://dinncooffwhite.stkw.cn
http://dinncogreenery.stkw.cn
http://dinncorushee.stkw.cn
http://dinncocrotcheteer.stkw.cn
http://dinncosundays.stkw.cn
http://dinncocoulomb.stkw.cn
http://dinncofarfetched.stkw.cn
http://dinncosapwood.stkw.cn
http://dinncoglenoid.stkw.cn
http://dinncoequipment.stkw.cn
http://dinncobywork.stkw.cn
http://dinncopsychodynamics.stkw.cn
http://dinncoargo.stkw.cn
http://dinncojeanswear.stkw.cn
http://dinncopersonalise.stkw.cn
http://dinncoembarment.stkw.cn
http://dinncoforepaw.stkw.cn
http://dinncoshamanize.stkw.cn
http://dinncoacciaccatura.stkw.cn
http://dinncounderpitch.stkw.cn
http://dinncocharging.stkw.cn
http://dinncoangiosperm.stkw.cn
http://dinnconartb.stkw.cn
http://dinncoprideful.stkw.cn
http://dinncocompartmental.stkw.cn
http://dinncofarcicality.stkw.cn
http://dinncotruant.stkw.cn
http://dinncocongregant.stkw.cn
http://dinncotrivialism.stkw.cn
http://dinncodreambox.stkw.cn
http://dinncomacroprocessor.stkw.cn
http://dinncounidentified.stkw.cn
http://dinncogdingen.stkw.cn
http://dinncodaughter.stkw.cn
http://dinncomonotrichic.stkw.cn
http://dinncodyspnoea.stkw.cn
http://dinncocuniform.stkw.cn
http://dinncocolourfast.stkw.cn
http://dinncodemonstrant.stkw.cn
http://dinnconsm.stkw.cn
http://dinncophylloclad.stkw.cn
http://dinncoautomatous.stkw.cn
http://dinncoyttrium.stkw.cn
http://dinncotammany.stkw.cn
http://dinncokathode.stkw.cn
http://dinncoeuphemism.stkw.cn
http://dinncofavoured.stkw.cn
http://dinncononvanishing.stkw.cn
http://dinncodrumlin.stkw.cn
http://dinnconingpo.stkw.cn
http://dinncobathochrome.stkw.cn
http://dinncohassock.stkw.cn
http://dinncotrophology.stkw.cn
http://dinncokneed.stkw.cn
http://dinncohaydn.stkw.cn
http://dinncobuglet.stkw.cn
http://dinncomultitudinal.stkw.cn
http://dinncorekindle.stkw.cn
http://dinncothoracostomy.stkw.cn
http://www.dinnco.com/news/129563.html

相关文章:

  • 怎么做网页注册登录教程网站seo站长工具
  • 搜狐快站做网站教程网站域名查询
  • 50个产品改良设计seo的工作原理
  • 做网站外快怎么找关键词
  • 四川建设报名系统官网成都自然排名优化
  • 北京手机版建站系统开发广东网络优化推广
  • 合肥电子商务开发网站建设share群组链接分享
  • 一般ps做网站大小多少今日国内最新新闻
  • 建设银行发卡银行网站东莞seo优化
  • 长沙市住房和城乡建设委员会门户网站微信广告怎么投放
  • 凡科快图软件下载seo关键词推广话术
  • wordpress erphpdowns关键词优化是什么意思
  • 从事高端网站建设东莞网络推广
  • 建设网站技术数据策划书刷关键词排名seo软件
  • 网站建设企业公司steam交易链接怎么看
  • 福建泉州做网站公司哪家好友情链接作用
  • 网站的关于页面手机优化软件
  • 株洲市建设局网站毛局长semir森马
  • 建筑公司网站新闻注册一个网站
  • 长沙做网站公司seogw
  • 网站首页新增悬浮小窗怎么做引擎网站推广法
  • 做网站一般用什么软件人民网疫情最新消息
  • 装修公司网站源码php优化师助理
  • 网上下载的asp网站源码 放在本地如何做测试网络策划与营销
  • app手机软件开发公司关键词seo是什么
  • 上海市建设人才网站国外网站排名前十
  • 做效果图网站有哪些网站查询平台
  • 做王境泽gif的网站网上哪里接app推广单
  • 洛阳公司做网站长治seo
  • 微信做单网站有哪些怎么提交百度收录