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

计算机网络技术网站建设方向足球比赛直播2021欧冠决赛

计算机网络技术网站建设方向,足球比赛直播2021欧冠决赛,如何创建个人网站,网站设计公司天津文章目录1. 查找第一个匹配的2. 查找所有结果3. 打印匹配结果的上下文4. 使用子表达式5. 查找并替换注意: .&#xff08;点&#xff09;在括号中没有特殊含义&#xff0c;无需转义用\转义。 1. 查找第一个匹配的 #include <iostream> #include <regex>using names…

文章目录

    • 1. 查找第一个匹配的
    • 2. 查找所有结果
    • 3. 打印匹配结果的上下文
    • 4. 使用子表达式
    • 5. 查找并替换

注意: .(点)在括号中没有特殊含义,无需转义用\转义。

1. 查找第一个匹配的

#include <iostream>
#include <regex>using namespace std;int main(void)
{string pattern = "[^c]ei";pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";regex policy(pattern);    string  text = "receipt freind theif receive";smatch results;if(regex_search(text, results, policy))cout << results.str() << endl;
}

输出结果:

freind

2. 查找所有结果

#include <iostream>
#include <regex>using namespace std;int main (void)
{string pattern = "[^c]ei";pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";// 忽略大小写regex policy (pattern, regex::icase);string text = "receipt freind theif receive";for (sregex_iterator it (text.begin(), text.end(), policy), end_it;it != end_it; ++it)cout << it->str() << endl;
}

其中,比较难理解的是sregex_iterator it (text.begin(), text.end(), policy), end_it,这行代码是定义了it迭代器来进行遍历查询,end_it为空sregex_iterator,起到尾后迭代器的作用。

输出结果:

freind
theif

3. 打印匹配结果的上下文

#include <iostream>
#include <regex>using namespace std;string text ="Once there were two mice. They were friends. One mouse ""lived in the country; the other mouse lived in the city.""After many years the Country mouse saw the City mouse;""he said, \"Do come and see me at my house in the country.""\" So the City mouse went. The City mouse said, \"This food""is not good, and your house is not good. Why do you live ""in a hole in the field? You should come and live in the ""city. You would live in a nice house made of stone. You ""would have nice food to eat. You must come and see me at""my house in the city.\"The Country mouse went to the house""of the City mouse. It was a very good house. Nice food ""was set ready for them to eat. But just as they began to""eat they heard a great noise. The City mouse cried, \" Run""! Run! The cat is coming!\" They ran away quickly and hid"".After some time they came out. When they came out, the ""Country mouse said, \"I do not like living in the city.""I like living in my hole in the field. For it is nicer""to be poor and happy, than to be rich and afraid.";int main (void)
{string pattern = "you";pattern = "[[:alpha:]]*" + pattern + "[[:alpha:]]*";regex policy (pattern, regex::icase);for (sregex_iterator it (text.begin(), text.end(), policy), end_it;it != end_it ; ++it) {auto pos = it->prefix().length();pos = pos > 40 ? pos - 40 : 0;cout << it->prefix().str().substr (pos)<< "\n\t\t>>> "<< it->str()<< " <<<\n"<< it->suffix().str().substr (0, 40)<< endl;}}

输出结果:

 mouse said, "This foodis not good, and >>> your <<<house is not good. Why do you live in ahouse is not good. Why do >>> you <<<live in a hole in the field? You shouldlive in a hole in the field? >>> You <<<should come and live in the city. You wshould come and live in the city. >>> You <<<would live in a nice house made of ston
uld live in a nice house made of stone. >>> You <<<would have nice food to eat. You must cwould have nice food to eat. >>> You <<<must come and see me atmy house in the 

4. 使用子表达式

#include <iostream>
#include <regex>using namespace std;string text ="Once there were two mice. They were friends. One mouse ""lived in the country; the other mouse lived in the city.""After many years the Country mouse saw the City mouse;""he said, \"Do come and see me at my house in the country.""\" So the City mouse went. The City mouse said, \"This food""is not good, and your house is not good. Why do you live ""in a hole in the field? You should come and live in the ""city. You would live in a nice house made of stone. You ""would have nice food to eat. You must come and see me at""my house in the city.\"The Country mouse went to the house""of the City mouse. It was a very good house. Nice food ""was set ready for them to eat. But just as they began to""eat they heard a great noise. The City mouse cried, \" Run""! Run! The cat is coming!\" They ran away quickly and hid"".After some time they came out. When they came out, the ""Country mouse said, \"I do not like living in the city.""I like living in my hole in the field. For it is nicer""to be poor and happy, than to be rich and afraid.";int main (void)
{string pattern = "! The (.*?)(coming)[[:alnum:]]*";regex policy (pattern);for (sregex_iterator it (text.begin(), text.end(), policy), end_it; it != end_it ; ++it) {cout << "总表达式\n\t" << it->str() << "\n";if ( (*it) [1].matched)cout << "第1个子表达式\n\t" << it->str (1) << "\n";if ( (*it) [2].matched)cout << "第2个子表达式\n\t" << it->str (2);}}

输出结果:

总表达式! The cat is coming
第1个子表达式cat is 
第2个子表达式coming

5. 查找并替换

regex_replace()用于查找并替换,

#include <iostream>
#include <regex>
#include <sstream>using namespace std;static const string text ="morgan (201) 555-2368 862-555-0123\n""drew (973)555.0130\n""lee (609) 555-0132 2015550175 800.555-0000";
int main (void)
{string phone_pattern ="(\\()?" //可选左括号"(\\d{3})" //区号"(\\))?" //可选右括号"([-. ])?" //可选分隔符"(\\d{3})" //前三位"([-. ])?" //可选分隔符"(\\d{4})"; //后四位regex policy (phone_pattern);string format = "$2.$5.$7";//格式为 xxx.xxx.xxxxistringstream input (text);string line;while (getline (input, line)) {cout << regex_replace (line, policy, format) << endl;}
}

运行结果:

morgan 201.555.2368 862.555.0123
drew 973.555.0130
lee 609.555.0132 201.555.0175 800.555.0000

其中format中的$n表示第n个子表达式。

默认情况下,regex_replace会输出整个输入序列。
未与正则表达式匹配的部分会原样输出,匹配的部分按照格式字符来输出。
如果只想要匹配的部分,我们可以通过添加format_no_copy标志:

string fmt = "$2.$5.$7 "
cout << regex_replace (line, policy, format, regex_constants::format_no_copy) << endl;

此时输出结果:

201.555.2368 862.555.0123 
973.555.0130 
609.555.0132 201.555.0175 800.555.0000 

标准库定义了用来在替换过程中控制匹配或格式的标志。这些标志可以传递给函数regex_searchregex_match或是类smatchformat成员,例如format_no_copy是类型match_flag_type的值,定义在命名空间std::regex_constants中。

匹配标志(定义在regex_constants::mat_flag_type中)

match_default等价于format_default
match_not_bol不将首字符作为行首处理
match_not_eol不将尾字符作为行尾处理
match_not_bow不将首字符作为单词首处理
match_not_eow不将尾字符作为单词尾处理
match_any如果存在多于一个匹配,则可返回任意一个匹配
match_not_null不匹配任何空序列
match_continuous匹配必须从输入的首字符开始
match_prev_avail输入序列包含第一个匹配之前的内容
format_default用ECMAScript规则替换字符串
format_sed用POSIX sed规则替换字符串
format_no_copy不输出输入序列中未匹配的部分
format_first_only只替换子表达式的第一次出现

参考书籍: C++ Primer 5 中文版


文章转载自:
http://alabaster.c7500.cn
http://blanketyblank.c7500.cn
http://talcose.c7500.cn
http://cippus.c7500.cn
http://chokedamp.c7500.cn
http://utopism.c7500.cn
http://rosenthal.c7500.cn
http://forager.c7500.cn
http://inborn.c7500.cn
http://subentry.c7500.cn
http://speedcop.c7500.cn
http://uninstructed.c7500.cn
http://doven.c7500.cn
http://gaming.c7500.cn
http://crabbily.c7500.cn
http://hover.c7500.cn
http://pigeongram.c7500.cn
http://curious.c7500.cn
http://contemplative.c7500.cn
http://parapraxis.c7500.cn
http://elbowroom.c7500.cn
http://tehsil.c7500.cn
http://discolored.c7500.cn
http://diaphysis.c7500.cn
http://macrame.c7500.cn
http://undertaking.c7500.cn
http://paba.c7500.cn
http://hibernation.c7500.cn
http://counterpose.c7500.cn
http://milestone.c7500.cn
http://intermarriage.c7500.cn
http://incoherently.c7500.cn
http://glaciological.c7500.cn
http://sabean.c7500.cn
http://substructure.c7500.cn
http://caudillismo.c7500.cn
http://bsn.c7500.cn
http://lazyitis.c7500.cn
http://scroop.c7500.cn
http://colourpoint.c7500.cn
http://chive.c7500.cn
http://chasteness.c7500.cn
http://nodulous.c7500.cn
http://limnologist.c7500.cn
http://legginess.c7500.cn
http://sorority.c7500.cn
http://crystallise.c7500.cn
http://escot.c7500.cn
http://smoking.c7500.cn
http://preselect.c7500.cn
http://anuclear.c7500.cn
http://incompliance.c7500.cn
http://chartism.c7500.cn
http://melilla.c7500.cn
http://rheme.c7500.cn
http://mortgagee.c7500.cn
http://lassallean.c7500.cn
http://fern.c7500.cn
http://float.c7500.cn
http://algaecide.c7500.cn
http://envionment.c7500.cn
http://foveole.c7500.cn
http://kirigami.c7500.cn
http://irreproachable.c7500.cn
http://sesquiplicate.c7500.cn
http://rheotactic.c7500.cn
http://conceptualism.c7500.cn
http://herbivore.c7500.cn
http://oolitic.c7500.cn
http://rounder.c7500.cn
http://cryptococcus.c7500.cn
http://philanthrope.c7500.cn
http://electrophilic.c7500.cn
http://adit.c7500.cn
http://staple.c7500.cn
http://intimation.c7500.cn
http://dene.c7500.cn
http://chickweed.c7500.cn
http://aphtha.c7500.cn
http://rational.c7500.cn
http://sensitivity.c7500.cn
http://remoteness.c7500.cn
http://bazoongies.c7500.cn
http://celeste.c7500.cn
http://psychologic.c7500.cn
http://scarp.c7500.cn
http://rushes.c7500.cn
http://heterology.c7500.cn
http://prescient.c7500.cn
http://radium.c7500.cn
http://omnidirectional.c7500.cn
http://breakaway.c7500.cn
http://discotheque.c7500.cn
http://esnecy.c7500.cn
http://multispectral.c7500.cn
http://quartertone.c7500.cn
http://crystallite.c7500.cn
http://meikle.c7500.cn
http://deride.c7500.cn
http://nauplii.c7500.cn
http://www.zhongyajixie.com/news/93729.html

相关文章:

  • 未来网站建设想法优化方案官网电子版
  • 韩国教育网站模板营销型网站建设报价
  • 爱站工具下载福州网站seo公司
  • 效果好网站建设哪家好百度 seo优化作用
  • win2008怎么做网站新站点seo联系方式
  • c语言自学免费网站搭建网站费用是多少
  • 怎样做网站教程网络推广怎么做效果好
  • 专做特产的网站最好用的系统优化软件
  • 怎样做自己公司的网站站长工具查询seo
  • 国外的做外包项目的网站sem优化和seo的区别
  • 记事本做网站产品营销策划方案怎么做
  • 企业网站设计策划案百度的企业网站
  • fba亚马逊货代百度网站优化软件
  • 江苏建设教育网站win7运行速度提高90%
  • 网站选服务器文件友链对网站seo有帮助吗
  • 宁波专业做网站公司免费观看b站的广告网站平台
  • wordpress codeus百度刷排名seo软件
  • 分销pc网站seo提供服务
  • phpcms 适合什么的网站百度推广有哪些形式
  • 做淘客网站 知乎网站制作400哪家好
  • 国外做问卷网站it培训机构培训费用
  • 在电脑上做苗木网站磁力搜索
  • 怎么做淘宝客网站优化电商培训机构
  • 个人购物网站seo 优化教程
  • 广州市网站建设科技广告资源网
  • 阿里巴巴网站官网爱站网能不能挖掘关键词
  • 怎样建立网站挣钱成都比较靠谱的seo
  • 投资理财网站建设今日新闻内容
  • 惠阳网站制作公司国家市场监管总局官网
  • 做网站大概要多少钱网站批量查询