boost::regex学习(2)

四:regex_match例子代码学习
1 我们经常会看一个字符串是不是合法的IP地址,合法的IP地址需要符合以下这个特征:
  xxx.xxx.xxx.xxx 其中xxx是不超过255的整数
正则表达式找到上面的这种形式的字符串相当容易,只是判断xxx是否超过255就比较困难了(因为正则表达式是处理的文本,而非数字)
OK,我们先来处理一个数字,即:xxx。找到一种表达式来处理这个数字,并且保证这个数字不会超过255
第一种情况:x,即只有一个数字,它可以是0~9 ,用\d 表示
第二种情况:xx,即有两个数字,它可以是00~99,用\d\d 表示
第三种情况:xxx,这种情况分为两种,一种是 1xx,可以用 1\d\d 表示
                                   另外一种是 2xx,这又分为两种 2[01234]\d
                                                             和 25[012345]
好了组合起来
1?\d{1,2}|2[01234]\d|25[012345]
既可以标识一个不大于255的数字字符串

嗯,我们现在需要重复这种情况既可:
(1?\d{1,2}|2[01234]\d|25[012345])\.(1?\d{1,2}|2[01234]\d|25[012345])\.(1?\d{1,2}|2[01234]\d|25[012345])\.(1?\d{1,2}|2[01234]\d|25[012345])

呵呵,长是长了点,我试图用boost支持的子表达式缩短,但是没有达到效果,请各位了解boost的正则表达式的达人指点:
(1?\d{1,2}|2[01234]\d|25[012345])\.\1$\.\1$\.\1$
(参看反向索引:http://www.boost.org/libs/regex/doc/syntax_perl.html
似乎反向只能匹配与第一个字符完全一样的字符串,与我们的需求不同)

Example:
std::string regstr = "(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])";
boost::regex expression(regstr);
std::
string testString = "192.168.4.1";
if( boost::regex_match(testString, expression) )
{
    std::cout
<< "This is ip address" << std::endl;
}
else
{
    std::cout
<< "This is not ip address" << std::endl;
}

2 我们来看看regex_match的另外一个函数原型
template <class ST, class SA, class Allocator, class charT, class traits>
    bool regex_match(const basic_string<charT, ST, SA>& s,
    match_results<typename basic_string<charT, ST, SA>::const_iterator, Allocator>& m,
    const basic_regex <charT, traits>& e, match_flag_type flags = match_default);

template <class BidirectionalIterator, class Allocator, class charT, class traits>
bool regex_match(BidirectionalIterator first, BidirectionalIterator last,
match_results<BidirectionalIterator, Allocator>& m,
const basic_regex <charT, traits>& e,
match_flag_type flags = match_default);
 
注意参数m,如果这个函数返回false的话,m无定义。如果返回true的话,m的定义如下

Element

Value

m.size()

e.mark_count()

m.empty()

false

m.prefix().first

first

m.prefix().last

first

m.prefix().matched

false

m.suffix().first

last

m.suffix().last

last

m.suffix().matched

false

m[0].first

first

m[0].second

last

m[0].matched

true if a full match was found, and false if it was a partial match (found as a result of the match_partial flag being set).

m[n].first

For all integers n < m.size(), the start of the sequence that matched sub-expression n. Alternatively, if sub-expression n did not participate in the match, then last.

m[n].second

For all integers n < m.size(), the end of the sequence that matched sub-expression n. Alternatively, if sub-expression n did not participate in the match, then last.

m[n].matched

For all integers n < m.size(), true if sub-expression n participated in the match, false otherwise.

Example:
std::string regstr = "(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])\\.(1?\\d{1,2}|2[01234]\\d|25[012345])";
boost::regex expression(regstr);
std::
string testString = "192.168.4.1";
boost::smatch what;
if( boost::regex_match(testString, what, expression) )
{
    std::cout
<< "This is ip address" << std::endl;
    
for(int i = 1;i <= 4;i++)
    {
        std::
string msg(what[i].first, what[i].second);
        std::cout
<< i << "" << msg.c_str() << std::endl;
    }
}
else
{
    std::cout
<< "This is not ip address" << std::endl;
}
这个例子会把所有的IP的单个数字答应出来:
This is ip address
1:192
2:168
3:4
4:1

转载于:https://www.cnblogs.com/shootingstars/archive/2007/08/01/838752.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380292.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++ 随机数

#include < iostream> #include < ctime> #include < cstdlib> using namespace std; int main () { int i,j; // 设置种子 srand( (unsigned)time( NULL ) ); /* 生成 10 个随机数 */ for( i 0; i < 10; i ) { // 生成实际的随机数 j rand…

远控免杀4---Evasion免杀

0x01 免杀能力一览表 1、下表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/meterperter/reverse_tcp模块生成。3、由于本机测试时只是安装了360全家桶和火绒&#xff0c;所以…

微内核和宏内核的区别_8086微处理器中的过程和宏之间的区别

微内核和宏内核的区别Prerequisite 先决条件 Procedure in 8086 Microprocessor 8086微处理器中的过程 Macros in 8086 Microprocessor 8086微处理器中的宏 过程和宏之间的区别 (Differences between Procedures and Macros ) CharacteristicProcedureMacroNumber of Instruct…

对决

描述 Topcoder 招进来了 n 个新同学&#xff0c;Yougth计划把这个n个同学分成两组&#xff0c;要求每组中每个人必须跟另一组中每个同学进行一次算法对决&#xff0c;问存不存在一种分组方式在k场完成对决。&#xff08;两组中每一组中人数都要大于0&#xff09; //注意&…

我的世界方块云服务器bug,我的世界:两个方块能无限刷经验?这装置太BUG了

在我的世界中&#xff0c;刷经验是一种很常见的事情&#xff0c;不过以往的刷经验机&#xff0c;常常需要建造刷怪塔刷怪&#xff0c;不仅麻烦&#xff0c;工程量大&#xff0c;怪物积累多了还会造成卡顿&#xff0c;非常不方便。但是随着我的世界更新了火炉这种物品之后&#…

远控免杀5---Veil免杀

0x01 免杀能力一览表 1、下表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/meterperter/reverse_tcp模块生成。3、由于本机测试时只是安装了360全家桶和火绒&#xff0c;所以…

ADO.NET_09_Using 关键字

本文内容 演示 Using 关键字验证 Using 关键字演示 Using 关键字 string connStr "data sourceora11; uidscott; pwdtiger; unicodetrue"; string sqlStr "SELECT * FROM EMP"; using (OracleConnection conn new OracleConnection(connStr)) { conn.Op…

js isinteger_在JavaScript中使用示例使用Number isInteger()方法

js isinteger编号isInteger()方法 (Number isInteger() Method) isInteger() is a Number Method, it is used to check whether a given number is an integer or not. isInteger()是一个数字方法&#xff0c;用于检查给定数字是否为整数。 It returns true if given number …

最新:日文.NET Framework 3.5 与 VS 2008 beta 2 已经公布

半个月以前,微软发布了Beta2版本的.NET Framework 3.5 与 Visual Studio 2008,可以从这里下载.而本周,代表双字节字符区域的第一个日文版本Beta2已经发布,现在同样也可以提供下载,这意味着中文版将在不久后与我们见面(按照微软软件的惯例,除微软中国研究院自主推出的软件外,中文…

三角形描边css,[CSS] tips带有描边的小箭头

linear-gradient和border:Document.box {position: relative;padding: 10px; /* 重要 防止内容被覆盖 */text-align: center;border: 1px solid #f60;border-radius: 5px;}.box::after {content: ;position: absolute;left: 50%;display: table;width: 10px;height: 10px;margi…

sql语句中出现笛卡尔乘积

没有join条件导致笛卡尔乘积 学过线性代数的人都知道&#xff0c;笛卡尔乘积通俗的说&#xff0c;就是两个集合中的每一个成员&#xff0c;都与对方集合中的任意一个成员有关联。可以想象&#xff0c;在SQL查询中&#xff0c;如果对两张表join查询而没有join条件时&#xff0c;…

远控免杀专题6---Venom免杀

0x01 免杀能力一览表 几点说明&#xff1a; 1、上表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。 2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/meterperter/reverse_tcp模块生成。 3、由于本机测试时只是安装了360全…

人工智能 自然语言处理_自然语言处理(NLP)| 人工智能

人工智能 自然语言处理Natural Language Processing (NLP) is one of the most successful applications of Artificial Intelligence in the real world. The Natural Language Processing in the terms of AI means that the Artificial Agents are able to able to understa…

精挑细选

描述 小王是公司的仓库管理员&#xff0c;一天&#xff0c;他接到了这样一个任务&#xff1a;从仓库中找出一根钢管。这听起来不算什么&#xff0c;但是这根钢管的要求可真是让他犯难了&#xff0c;要求如下&#xff1a; 1、 这根钢管一定要是仓库中最长的&#xff1b; 2、 这根…

hoho,我要坚持写blog

争取一天一篇,提高技术. 转载于:https://www.cnblogs.com/lbq1221119/archive/2007/08/17/859183.html

查看ajax传来的数据,jQuery AJAX 方法 success()后台传来的4种数据

1.后台返回一个页面js代码/**(1)用$("#content-wrapper").html(data);显示页面*/$.ajax({async : false,cache : false,type : POST,url : area/prepareCreate,error : function() {alert(smx失败 );},success : function(data) {$("#content-wrapper").ht…

远控免杀专题7 ---shellter免杀

0x01 免杀能力一览表 几点说明&#xff1a; 1、上表中标识 √ 说明相应杀毒软件未检测出病毒&#xff0c;也就是代表了Bypass。 2、为了更好的对比效果&#xff0c;大部分测试payload均使用msf的windows/meterperter/reverse_tcp模块生成。 3、由于本机测试时只是安装了360全…

Notepad++高亮AS文件

如果编写ActionScript&#xff0c;可以选择语言->Flash actionScript 但是软件似乎不能确定as文件使用的语言&#xff0c;需要修改软件配置 Notepad 有 ActionScript 的语法高亮功能, 但它似乎不能被程序探查到. 打开的每个 AS 文件, 都要自行套用语法高亮, 特别麻烦. 当你打…

python 示例_Python日历类| yeardayscalendar()方法与示例

python 示例Python Calendar.yeardayscalendar()方法 (Python Calendar.yeardayscalendar() Method) Calendar.yeardayscalendar() method is an inbuilt method of the Calendar class of calendar module in Python. It uses an instance of this class and returns the list…

获取两个数的最大值,判断是否相等;

package asdwwssq; import java.util.Scanner;//引入扫描仪Scanner,创建键盘录入对象&#xff1b; public class qweqwe { public static void main(String[] args) { Scanner anew Scanner(System.in);//将一个新扫描仪赋给a&#xff1b; System.out.println(“请输入第一…