前面介绍了 std::regex来存储正则表达式,

std::regex_match和std::regex_search来使表达式和指定字符串配对.

但是在std::regex_match和std::regex_search匹配到字符串的时候会把匹配的结果存放到std::match_resultsstd::sub_match.

  • std::match_results:
template<class BidirIt,class Alloc = std::allocator<std::sub_match<BidirIt>>
         > class match_results;

由上面的声明可以看出来std::match_results的构造函数不接受任何参数,但是我们必须提供模板参数:

BidirIt的两种提供方式:

1,std::match_results<std::basic_string<char>::const_iterator>;

2,std::match_results<const char*>;

另外我们也可以提供一个allocator!

还有需要我们注意的是我们提供BidirIt的时候注意要么是const_iterator要么就是const char*,为什么会这样呢?翻看了一下介绍发现匹配的结果其实是 指向被匹配的字符串的迭代器 也就是说 如果被匹配的字符串 被销毁了那么 匹配的结果也就不能用了.

std::match_results::match_results

//可以提供一个allocator.
explicit match_results( const Allocator& a = Allocator() );

//拷贝构造函数
match_results( const match_results& rhs );

//移动构造函数.
match_results( match_results&& rhs );

std::match_results::operator=

match_results& operator=( const match_results& other );
	
match_results& operator=( match_results&& other );

std::match_results::get_allocator

//返回指定的allocator.
allocator_type get_allocator() const;

std::match_results::ready

bool ready() const;

检查std::match_results的状态,默认情况下是not ready的,只有在匹配了regex后才是ready的.

std::match_results::empty

bool empty() const;

检查匹配时候成功,不成功则为empty的返回true.

std::match_results:: size

size_type size() const;

返回匹配到的字符串中子字符串的数目.

#include <iostream>
#include <regex>

int main ()
{
  std::string mystring ("subject");
  std::smatch mymatches;
  std::regex myregex ("(sub)(.*)");

  std::regex_match ( mystring,mymatches,myregex );

  std::cout << mymatches.size() << " matches found:" << std::endl;
  for (unsigned i=0; i<mymatches.size(); ++i)
    std::cout << "match #" << i << ": " << mymatches[i] << std::endl;

  return 0;
}



3 matches found:
match #0: subject
match #1: sub
match #2: ject

std::match_results::max_size

size_type max_size() const;

返回std::match_results可容纳的最多的子表达式的数量.

std::match_results::length

difference_type length( size_type n = 0 ) const;

返回匹配到的第n个子字符串的长度. 默认为0返回匹配到的字符串的长度.

std::match_results::positon

difference_type position( size_type n = 0 ) const;

将返回 std::distance(prefix().first,(*this)[sub].first);

std::match_results::str

string_type str( size_type n = 0 ) const;

返回匹配到的字符串中的第n个子字符串.

std::match_results::operator[]

const_reference operator[]( size_type n ) const;

返回匹配到的字符串中的第n个子字符串. 也请注意这里的const_reference其实是对std::sub_match.

std::match_results::prefix

const_reference prefix() const;

返回匹配到的字符串的前缀(也就是前面未被匹配到的部分).也请注意这里的const_reference其实是对std::sub_match.

std::match_results::suffix

const_reference suffix() const;

返回匹配到的字符串的后面的未被匹配到的字符串. 也请注意这里的const_reference其实是对std::sub_match.

std::match_results::format

template< class OutputIt >

OutputIter format( OutputIt out,const char_type* fmt_first,const char_type* fmt_last,std::regex_constants::match_flag_type flags =
                       std::regex_constants::format_default ) const;

template< class OutputIt,class ST,class SA >
OutputIter format( OutputIt out,const basic_string<char_type,ST,SA>& fmt,std::regex_constants::match_flag_type flags =
                       std::regex_constants::format_default ) const;

template< class ST,class SA >
std::basic_string<char_type,SA>
    format( const std::basic_string<char_type,std::regex_constants::match_flag_type flags =
                std::regex_constants::format_default ) const;
	
string_type format( const char_type* fmt_s,std::regex_constants::match_flag_type flags =
                        std::regex_constants::format_default ) const;

有点类似std::regex_replace.看一个demo:

#include <iostream>
#include <string>
#include <regex>
 
int main()
{
    std::string s = "for a good time,call 867-5309";
    std::regex phone_regex("\\d{3}-\\d{4}");
    std::smatch phone_match;
 
    if (std::regex_search(s,phone_match,phone_regex)) {
        std::string fmt_s = phone_match.format(
            "$`"    // $` means characters before the match
            "[$&]"  // $& means the matched characters
            "$'");  // $' means characters following the match
        std::cout << fmt_s << '\n';
    }   
}
  • std::sub_match

std::sub_match::sub_match

template<class BidirIt> class sub_match;

也有两种指定模板参数的方式:

1,std::sub_match<std::basic_string<char>::const_iterator>;

2,std::sub_match<const char*>;

std::sub_match::length

difference_type length() const;

返回字符串的长度.

std::sub_match::operator string_type 和 std::sub_match::str()效果一样都是返回一个字符串.

Regex: 正则表达式(3).的更多相关文章

  1. HTML5数字输入仅接受整数的实现代码

    这篇文章主要介绍了HTML5数字输入仅接受整数的实现代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  2. ios – 使用大写符号在字符串swift中获取URL的正则表达式

    我尝试在文本中获取URL.所以,在此之前,我使用了这样一个表达式:但是当用户输入带有大写符号的URL时(例如Http://Google.com,它与它不匹配)我遇到了问题.我试过了:但什么都没发生.解决方法您可以使用正则表达式中的i内联标志关闭区分大小写,有关可用正则表达式功能的详细信息,请参阅FoundationFrameworkReference.(?ismwx-ismwx)Flagsetti

  3. ios – 如何在Swift 3中使用正则表达式?

    解决方法我相信.当没有其他选项适用时,将使用.allZeros.因此,使用Swift3,您可以传递一个空的选项列表或省略options参数,因为它默认为无选项:要么请注意,在Swift3中,您不再使用error参数.它现在抛出.

  4. ios – lldb断点在类目标c中的所有方法

    如何使用lldb在ObjectiveC类中的所有方法上自动设置断点?

  5. swift的正则表达式(NSRegularExpression)

    init(_pattern:String){varerror:NSError?

  6. swift 正则表达式运用实例选自《swifter 100个swift开发必备tip 》

  7. Swift截取HTML中的所有图片url

    在Swift中,要从HTML格式的String中截取出所有的img的所有图片url,要截取的url就要匹配url,需要用到正则表达式。

  8. 收藏swift正则表达式的各种验证

    1.验证邮箱classfuncvalidateEmail(email:String)->Bool{varemailString="[A-Z0-9a-z._%-]@[A-Za-z0-9.-]\\.[A-Za-z]{2,4}"varemailPredicate=nspredicate(format:"SELFMATCHES%@",emailString)returnemailPredicate.eva

  9. Swift3.0-正则表达式 &lt;待续&gt;

    贡献者:赵大财博客:https://my.oschina.net/zhaodacaiGitHub:https://github.com/zhaodacai邮箱:zhaodacai@yeah.comQQ:327532817=============================先直接来代码:NSRegularExpression.OptionscaseInsensitive不区分大小写allowC

  10. swift – 使用“如果让…”与许多表达式

    Swift1.2更新从Swift1.2开始,如果允许允许展开多个可选项,那么现在可以写下这个,如下例所示:您甚至可以交换条件,如:以前在Swift1.2之前有效没有一个丑陋的力量包装,你可以这样做:实际上仍然很冗长这是因为可选类型的表单类型?实际上是可选的的缩写,这是一个大致如下的枚举:然后,您可以使用模式匹配作为任何其他枚举。

随机推荐

  1. 法国电话号码的正则表达式

    我正在尝试实施一个正则表达式,允许我检查一个号码是否是一个有效的法国电话号码.一定是这样的:要么:这是我实施的但是错了……

  2. 正则表达式 – perl分裂奇怪的行为

    PSperl是5.18.0问题是量词*允许零空间,你必须使用,这意味着1或更多.请注意,F和O之间的空间正好为零.

  3. 正则表达式 – 正则表达式大于和小于

    我想匹配以下任何一个字符:或=或=.这个似乎不起作用:[/]试试这个:它匹配可选地后跟=,或者只是=自身.

  4. 如何使用正则表达式用空格替换字符之间的短划线

    我想用正则表达式替换出现在带空格的字母之间的短划线.例如,用abcd替换ab-cd以下匹配字符–字符序列,但也替换字符[即ab-cd导致d,而不是abcd,因为我希望]我如何适应以上只能取代–部分?

  5. 正则表达式 – /bb | [^ b] {2} /它是如何工作的?

    有人可以解释一下吗?我在t-shirt上看到了这个:它似乎在说:“成为或不成为”怎么样?我好像没找到’e’?

  6. 正则表达式 – 在Scala中验证电子邮件一行

    在我的代码中添加简单的电子邮件验证,我创建了以下函数:这将传递像bob@testmymail.com这样的电子邮件和bobtestmymail.com之类的失败邮件,但是带有空格字符的邮件会漏掉,就像bob@testmymail也会返回true.我可能在这里很傻……当我测试你的正则表达式并且它正在捕捉简单的电子邮件时,我检查了你的代码并看到你正在使用findFirstIn.我相信这是你的问题.findFirstIn将跳转所有空格,直到它匹配字符串中任何位置的某个序列.我相信在你的情况下,最好使用unapp

  7. 正则表达式对小字符串的暴力

    在测试小字符串时,使用正则表达式会带来性能上的好处,还是会强制它们更快?不会通过检查给定字符串的字符是否在指定范围内比使用正则表达式更快来强制它们吗?

  8. 正则表达式 – 为什么`stoutest`不是有效的正则表达式?

    isthedelimiter,thenthematch-only-onceruleof?PATTERN?

  9. 正则表达式 – 替换..与.在R

    我怎样才能替换..我尝试过类似的东西:但它并不像我希望的那样有效.尝试添加fixed=T.

  10. 正则表达式 – 如何在字符串中的特定位置添加字符?

    我正在使用记事本,并希望使用正则表达式替换在字符串中的特定位置插入一个字符.例如,在每行的第6位插入一个逗号是什么意思?如果要在第六个字符后添加字符,请使用搜索和更换从技术上讲,这将用MatchGroup1替换每行的前6个字符,后跟逗号.

返回
顶部