四: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