C++ Boost

Regex++ template class reference.

Copyright (c) 1998-2001

Dr John Maddock

Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. Dr John Maddock makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.


class regbase

#include <boost/regex.hpp>

regbase¥¯¥é¥¹¤Ïreg_expression¤Î¤¿¤á¤Î¥Æ¥ó¥×¥ì¡¼¥È°ú¿ô¤Ë°Í¸¤·¤Ê¤¤´ðÄ쥯¥é¥¹¤Ç¤¢¤ë¡£ Í£°ì¤Î¸ø³«¥á¥ó¥Ð¤Ï Îóµó·¿¤ÎÃÍ flag_type ¤Ç¤¢¤ê¡¢¤³¤ì¤ÏÀµµ¬É½¸½¤¬¤É¤Î¤è¤¦¤Ë²ò¼á ¤µ¤ì¤ë¤«¤ò·èÄꤹ¤ë

class regbase
{
public:
   enum flag_type_
   {
      escape_in_lists = 1,                          // '\\' special inside [...] 
      char_classes = escape_in_lists << 1,          // [[:CLASS:]] allowed 
      intervals = char_classes << 1,                // {x,y} allowed 
      limited_ops = intervals << 1,                 // all of + ? and | are normal characters 
      newline_alt = limited_ops << 1,               // \n is the same as | 
      bk_plus_qm = newline_alt << 1,                // uses \+ and \? 
      bk_braces = bk_plus_qm << 1,                  // uses \{ and \} 
      bk_parens = bk_braces << 1,                   // uses \( and \) 
      bk_refs = bk_parens << 1,                     // \d allowed 
      bk_vbar = bk_refs << 1,                       // uses \| 
      use_except = bk_vbar << 1,                    // exception on error 
      failbit = use_except << 1,                    // error flag 
      literal = failbit << 1,                       // all characters are literals 
      icase = literal << 1,                         // characters are matched regardless of case 
      nocollate = icase << 1,                       // don't use locale specific collation 

      basic = char_classes | intervals | limited_ops | bk_braces | bk_parens | bk_refs,
      extended = char_classes | intervals | bk_refs,
      normal = escape_in_lists | char_classes | intervals | bk_refs | nocollate,
      emacs = bk_braces | bk_parens | bk_refs | bk_vbar,
      awk = extended | escape_in_lists,
      grep = basic | newline_alt,
      egrep = extended | newline_alt,
      sed = basic,
      perl = normal
   }; 
   typedef unsigned int flag_type;
};   

 
 
 

Îóµó·¿ regbase::flag_type ¤ÏÀµµ¬É½¸½¤Î¥³¥ó¥Ñ¥¤¥ë¤Î¤¿¤á¤Î¹½Ê¸µ¬Â§¤ò·èÄꤹ¤ë¡£ ¤½¤ì¤¾¤ì¤Î¥Õ¥é¥°¤Ï°Ê²¼¤Î¤è¤¦¤ÊºîÍѤò»ý¤Ä:
 

  regbase::escape_in_lists ʸ»ú½¸¹ç¤ÎÃæ¤Ç "\" ¤ò»È¤¦¤³¤È¤òǧ¤á¤ë¡£ ʸ»ú½¸¹ç¤ÎÃæ¤Îʸ»ú¡¢Î㤨¤Ð [\]] ¤Ï "]" ¤Î¤ß¤ò´Þ¤àʸ»ú½¸¹ç¤òɽ¤·¤Æ¤¤¤ë¡£ ¤â¤·¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢ "\" ¤Ïʸ»ú½¸¹ç¤ÎÃæ¤Ç¤ÏÄ̾ï¤Îʸ»ú¤È¤·¤Æ°·¤ï¤ì¤ë¡£
  regbase::char_classes ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ ʸ»ú¥¯¥é¥¹ [:¥¯¥é¥¹Ì¾:] ¤òʸ»ú½¸¹çÀë¸À¤ÎÃæ ¤Ç»È¤¦¤³¤È¤òǧ¤á¤ë¡£ Î㤨¤Ð [[:word:]] ¤Ïʸ»ú¥¯¥é¥¹ "word"¤Ë°¤¹¤ëÁ´¤Æ¤Îʸ»ú¤Î ½¸¹ç¤òɽ¤·¤Æ¤¤¤ë¡£  
  regbase:: intervals ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ ·«¤êÊÖ¤·²ó¿ô¤ÎÈϰϻØÄê¤ò»È¤¦¤³¤È¤òǧ¤á¤ë¡£ Î㤨¤Ð "a{2,4}" ¤Ïʸ»ú a ¤¬2²ó°Ê¾å4²ó°Ê²¼·«¤êÊÖ¤µ¤ì¤ë¤³¤È¤òɽ¤·¤Æ¤¤¤ë¡£  
  regbase:: limited_ops ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ "+"¡¢ "?" ¤½¤·¤Æ "|" ¤Ï¤¢¤é¤æ¤ë¾õ¶·¤Ç Ä̾ï¤Îʸ»ú¤È¤·¤Æ°·¤ï¤ì¤ë¡£  
  regbase:: newline_alt ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ ²þ¹Ôʸ»ú "\n" ¤ÏÇÓ¾±é»»»Ò "|"¤ÈƱ¤¸ºîÍѤò»ý¤Ä¡£  
  regbase:: bk_plus_qm ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ "\+" ¤Ï1²ó°Ê¾å¤Î·«¤êÊÖ¤·¤òɽ¤¹¡£¤½¤·¤Æ "\?" ¤Ï0²ó¤«1²ó¤Î·«¤êÊÖ¤·¤òɽ¤¹¡£ ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤Ê¤¤¤È¤­¤Ï¡¢Âå¤ï¤ê¤Ë "+" ¤È "?" ¤¬»È¤ï¤ì¤ë¡£  
  regbase:: bk_braces ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ "\{" ¤È "\}" ¤¬·«¤êÊÖ¤·¤Î²ó¿ô»ØÄê¤Ë»È¤ï¤ì¡¢ "{" ¤È "}" ¤ÏÄ̾ï¤Îʸ»ú¤È¤·¤Æ°·¤ï¤ì¤ë¡£ ¤³¤ì¤Ï¥Ç¥Õ¥©¥ë¥È¤È¤ÏÈ¿ÂÐ¤ÎÆ°ºî¤Ç¤¢¤ë¡£  
  regbase:: bk_parens ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ "\(" ¤È "\)" ¤¬»Òɽ¸½¤Î¥°¥ë¡¼¥×²½¤Ë»È¤ï¤ì¡¢ "(" ¤È ")" ¤ÏÄ̾ï¤Îʸ»ú¤È¤·¤Æ°·¤ï¤ì¤ë¡£ ¤³¤ì¤Ï¥Ç¥Õ¥©¥ë¥È¤È¤ÏÈ¿ÂÐ¤ÎÆ°ºî¤Ç¤¢¤ë¡£  
  regbase:: bk_refs ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ ¸åÊý»²¾È¤¬µö¤µ¤ì¤ë¡£  
  regbase:: bk_vbar ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ "\|" ¤¬ÇÓ¾±é»»»Ò¤òɽ¤·¡¢ "|" ¤ÏÄ̾ï¤Îʸ»ú¤È¤·¤Æ°·¤ï¤ì¤ë¡£ ¤³¤ì¤Ï¥Ç¥Õ¥©¥ë¥È¤È¤ÏÈ¿ÂÐ¤ÎÆ°ºî¤Ç¤¢¤ë¡£  
  regbase:: use_except ¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ bad_expression Îã³°¤¬¥¨¥é¡¼»þ¤ËȯÀ¸¤¹¤ë¡£ ¤³¤Î¥Õ¥é¥°¤ò»È¤¦¤³¤È¤Ï»¿À®¤µ¤ì¤Ê¤¤¡£¤Ê¤¼¤Ê¤é reg_expression ¤Ï¥¨¥é¡¼¤ÎºÝ¡¢ ¾ï¤ËÎã³°¤òȯÀ¸¤µ¤»¤ë¤À¤í¤¦¡£  
  regbase:: failbit ¤â¤· regbase::use_except ¤¬ÀßÄꤵ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢ ¥¨¥é¡¼»þ¤Ë¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤ë¡£»È¤¦Á°¤Ë¡¢Àµµ¬É½¸½¤¬ÂÅÅö¤«¤É¤¦¤«¤ò¸«¤ë¤¿¤á¤Ë ¤³¤Î¥Ó¥Ã¥È¤ò¥Á¥§¥Ã¥¯¤¹¤Ù¤­¤Ç¤¢¤ë¡£  
  regbase::literal ʸ»úÎóÃæ¤ÎÁ´¤Æ¤Îʸ»ú¤Ï¥ê¥Æ¥é¥ë¤È¤·¤Æ°·¤ï¤ì¤ë¡£ ÆÃ¼ìʸ»ú¤ä¡¢¥¨¥¹¥±¡¼¥×¥·¡¼¥±¥ó¥¹¤Ï¸ºß¤·¤Ê¤¤¡£  
  regbase::icase ʸ»úÎóÃæ¤ÎÁ´¤Æ¤Îʸ»ú¤Ï¡¢Âçʸ»ú/¾®Ê¸»ú¤Î¶èÊ̤ʤ¯°ìÃפ¹¤ë¡£  
  regbase::nocollate ʸ»ú½¸¹çÀë¸À¤ÎÃæ¤Ç¡¢Ê¸»úÈϰϤò°·¤¦¤È¤­¤Ë¡¢ ¥í¥±¡¼¥ë¤ËÆÃ¼ì¤Ê°ìÃפÏÉÔ²Äǽ¤Ë¤Ê¤ë¡£ Î㤨¤Ð¡¢¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ [a-c] ¤È¤¤¤¦É½¸½¤Ï a, b ¤½¤·¤Æ c ¤È¤¤¤¦Ê¸»ú¤Ë ¥í¥±¡¼¥ë¤Ë¤«¤«¤ï¤é¤º°ìÃפ¹¤ë¡£¤³¤Î¥Ó¥Ã¥È¤¬ÀßÄꤵ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢ [a-c] ¤Ï a ¤«¤é c ¤ÎʤӤˤ¢¤ë¤É¤ó¤Êʸ»ú¤Ë¤â°ìÃפ¹¤ë¡£  
  regbase::basic POSIX ¤Î´ðËÜÀµµ¬É½¸½¹½Ê¸¤È¤Î¸ß´¹À­¤ò¤â¤Ä: ¤Ä¤Þ¤ê¡¢char_classes | intervals | limited_ops | bk_braces | bk_parens | bk_refs ¤Î¥Ó¥Ã¥È¤òÀßÄꤹ¤ë¡£  
  Regbase::extended POSIX ¤Î³ÈÄ¥Àµµ¬É½¸½¹½Ê¸¤È¤Î¸ß´¹À­¤ò¤â¤Ä: ¤Ä¤Þ¤ê¡¢ char_classes | intervals | bk_refs ¤Î¥Ó¥Ã¥È¤òÀßÄꤹ¤ë¡£  
  regbase::normal ¤³¤ì¤Ï¥Ç¥Õ¥©¥ë¥È¤ÎÀßÄê¤Ç¤¢¤ë¡£ ¤½¤·¤ÆÂ¿¤¯¤Î¿Í¤¬¡¢¥é¥¤¥Ö¥é¥ê¤¬¤³¤Î¤è¤¦¤Ëưºî¤¹¤ë¤³¤È¤ò´üÂÔ¤·¤Æ¤¤¤ë¡£ POSIX ³ÈÄ¥¹½Ê¸¤È¤Î¸ß´¹À­¤ò¤â¤Ä¤¬¡¢¥í¥±¡¼¥ë¤ËÆÃ¼ì¤Ê°ìÃפʤ¤¡£ ¤½¤·¤ÆÊ¸»ú½¸¹çÀë¸À¤ÎÃæ¤Ç¤Î¥¨¥¹¥±¡¼¥×ʸ»ú¤ò»È¤¦¤³¤È¤¬½ÐÍè¤ë¡£ ¤³¤ì¤Ï regbase::escape_in_lists | regbase::char_classes | regbase::intervals | regbase::bk_refs | regbase::nocollate ¤Î¥Ó¥Ã¥È¤òÀßÄꤹ¤ë¡£  
  regbase::emacs emacs ¥¨¥Ç¥£¥¿¤È¤Î¸ß´¹À­¤òÍ¿¤¨¤ë¡£ bk_braces | bk_parens | bk_refs | bk_vbar ¤ÈÅù²Á¤Ç¤¢¤ë¡£  
  regbase::awk Unix ¤Î¥æ¡¼¥Æ¥£¥ê¥Æ¥£ Awk ¤È¤Î¸ß´¹À­¤òÍ¿¤¨¤ë¡£ POSIX ³ÈÄ¥Àµµ¬É½¸½¤ÈƱ¤¸¤À¤¬¡¢Ê¸»ú½¸¹ç¤ÎÃæ¤Ë¥¨¥¹¥±¡¼¥×¤òµö¤¹¡£ extended | escape_in_lists ¤ÈÅù²Á¤Ç¤¢¤ë¡£  
  regbase::grep Unix ¤Î grep ¥æ¡¼¥Æ¥£¥ê¥Æ¥£¤È¤Î¸ß´¹À­¤òÍ¿¤¨¤ë¡£ POSIX ´ðËÜÀµµ¬É½¸½¤ÈƱ¤¸¤À¤¬¡¢²þ¹Ôʸ»ú¤ÈÇÓ¾±é»»»Ò¤ÏÅù²Á¤Ç¤¢¤ë¡£ ¤Ä¤Þ¤ê¡¢ basic | newline_alt ¤ÈƱ¤¸¤Ç¤¢¤ë¡£  
  regbase::egrep Unix ¤Î egrep ¥æ¡¼¥Æ¥£¥ê¥Æ¥£¤È¤Î¸ß´¹À­¤òÍ¿¤¨¤ë¡£ POSIX ³ÈÄ¥Àµµ¬É½¸½¤ÈƱ¤¸¤À¤¬¡¢²þ¹Ôʸ»ú¤ÈÇÓ¾±é»»»Ò¤ÏÅù²Á¤Ç¤¢¤ë¡£ ¤Ä¤Þ¤ê¡¢ extended | newline_alt ¤ÈƱ¤¸¤Ç¤¢¤ë¡£  
  regbase::sed Unix ¤Î sed ¥æ¡¼¥Æ¥£¥ê¥Æ¥£¤È¤Î¸ß´¹À­¤òÍ¿¤¨¤ë¡£ POSIX ´ðËÜÀµµ¬É½¸½¤ÈƱ¤¸¤Ç¤¢¤ë¡£  
  regbase::perl ¥×¥í¥°¥é¥ß¥ó¥°¸À¸ì perl ¤È¤Î¸ß´¹À­¤òÍ¿¤¨¤ë¡£ regbase::normal ¤ÈƱ¤¸¤Ç¤¢¤ë¡£  

Exception classes.

#include <boost/pat_except.hpp>

¸í¤Ã¤¿Àµµ¬É½¸½¤¬¸½¤ì¤¿¤È¤­¤Ë¤¤¤Ä¤Ç¤â¡¢bad_expression ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤¬Åꤲ¤é¤ì¤ë¡£

namespace boost{

class bad_pattern : public std::runtime_error
{
public:
   explicit bad_pattern(const std::string& s) : std::runtime_error(s){};
};

class bad_expression : public bad_pattern
{
public:
   bad_expression(const std::string& s) : bad_pattern(s) {}
};


} // namespace boost

Êä­: bad_pattern ¥¯¥é¥¹¤ÏÁ´¤Æ¤Î¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¥ó¥°¤Ç¤ÎÎã³°¤Î ´ðÄ쥯¥é¥¹¤Ç¤¢¤ê¡¢ bad_expression ¤Ï¤½¤Î¤Ò¤È¤Ä¤Ç¤¢¤ë¡£ bad_pattern ¤Î´ðÄ쥯¥é¥¹¤È¤·¤Æ std::runtime_error ¤òÁªÂò¤·¤Æ¤¤¤ë¤³¤È¤Ë¤ÏµÄÏÀ¤Î;ÃϤ¬¤¢¤ë¡£ ¥é¥¤¥Ö¥é¥ê¤¬¤É¤¦»È¤ï¤ì¤ë¤«¤è¤ë¤¬¡¢Îã³°¤ÏÏÀÍýŪ¥¨¥é¡¼(¥×¥í¥°¥é¥Þ¤¬Í¿¤¨¤¿Àµµ¬É½¸½)¤«¡¢ ¼Â¹Ô»þ¥¨¥é¡¼(¥æ¡¼¥¶¤¬Í¿¤¨¤¿Àµµ¬É½¸½)¤Î¤¤¤º¤ì¤«¤À¤í¤¦¡£


Class reg_expression

#include <boost/regex.hpp>

¥Æ¥ó¥×¥ì¡¼¥È¥¯¥é¥¹ reg_expression ¤ÏÀµµ¬É½¸½¤Î²ò¼á¤È¥³¥ó¥Ñ¥¤¥ë¤ò¥«¥×¥»¥ë²½¤·¤Æ¤¤¤ë¡£ ¤³¤Î¥¯¥é¥¹¤Ï regbase ¤ò·Ñ¾µ¤·¤Æ¤¤¤Æ¡¢3¤Ä¤Î¥Æ¥ó¥×¥ì¡¼¥È¥Ñ¥é¥á¡¼¥¿¤ò»ý¤Ä¡£ ¥Ñ¥é¥á¡¼¥¿¤Ï:

charT: ʸ»ú·¿¤ò·èÄꤹ¤ë¡£ ¤Ä¤Þ¤ê¡¢ char ¤« wchar_t ¤Î¤É¤Á¤é¤«¤Ç¤¢¤ë¡£

traits: ʸ»ú·¿¤Î¿¶¤ëÉñ¤¤¤ò·èÄꤹ¤ë¡£Î㤨¤Ð¡¢ Âçʸ»ú¾®Ê¸»ú¤ò¹Íθ¤·¤Æ¾È¹ç¤¹¤ë¤«¡¢Ê¸»ú¥¯¥é¥¹Ì¾¤òǧ¼±¤¹¤ë¤«¡¢¤Ê¤É¤Ç¤¢¤ë¡£ ¥Ç¥Õ¥©¥ë¥È¤Î traits ¥¯¥é¥¹¤¬Í¿¤¨¤é¤ì¤Æ¤¤¤ë: regex_traits<charT> ¡£

Allocator: allocator ¥¯¥é¥¹¤Ï¡¢¥á¥â¥ê³ä¤êÅö¤Æ¤Ë»È¤ï¤ì¤ë¥¯¥é¥¹¤Ç¤¢¤ë¡£

´Êñ¤Ë»È¤¨¤ë¤è¤¦¤Ë¡¢2¤Ä¤Îɸ½àŪ¤Ê reg_expression ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òÄêµÁ¤·¤¿¡¢ 2¤Ä¤Î typedef ¤¬¤¢¤ë¡£ ¥«¥¹¥¿¥à¤Î¥¢¥í¥±¡¼¥¿¥¯¥é¥¹¤ò»È¤¤¤¿¤¤¤Î¤Ç¤Ê¤±¤ì¤Ð¡¢¤³¤ì¤é°Ê³°¤Î¤â¤Î¤ò»È¤¦É¬ÍפϤʤ¤¤À¤í¤¦¡£

namespace boost{
template <class charT, class traits = regex_traits<charT>, class Allocator = std::allocator<charT>  >
class reg_expression;
typedef reg_expression<char> regex;
typedef reg_expression<wchar_t> wregex;
}

reg_expression ¤ÎÄêµÁ¤Ï¼¡¤ÎÄ̤ê¤Ç¤¢¤ë: ¤³¤ÎÄêµÁ¤Ï basic_string ¥¯¥é¥¹¤Ë¸·Ì©¤Ë´ð¤Å¤¤¤Æ¤¤¤Æ¡¢charT ¤Î¥³¥ó¥Æ¥Ê¤È¤·¤Æ¤ÎÍ×µá¤òËþ¤¿¤·¤Æ¤¤¤ë¡£

namespace boost{
template <class charT, class traits = regex_traits<charT>, class Allocator = std::allocator<charT>  >
class reg_expression : public regbase
{
public: 
   // typedefs:  
   typedef charT char_type; 
   typedef traits traits_type; 
   // locale_type 
   // placeholder for actual locale type used by the 
   // traits class to localise *this. 
   typedef typename traits::locale_type locale_type; 
   // value_type 
   typedef charT value_type; 
   // reference, const_reference 
   typedef charT& reference; 
   typedef const charT& const_reference; 
   // iterator, const_iterator 
   typedef const charT* const_iterator; 
   typedef const_iterator iterator; 
   // difference_type 
   typedef typename Allocator::difference_type difference_type; 
   // size_type 
   typedef typename Allocator::size_type size_type; 
   // allocator_type 
   typedef Allocator allocator_type; 
   typedef Allocator alloc_type; 
   // flag_type 
   typedef boost::int_fast32_t flag_type; 
public: 
   // constructors 
   explicit reg_expression(const Allocator& a = Allocator()); 
   explicit reg_expression(const charT* p, flag_type f = regbase::normal, const Allocator& a = Allocator()); 
   reg_expression(const charT* p1, const charT* p2, flag_type f = regbase::normal, const Allocator& a = Allocator()); 
   reg_expression(const charT* p, size_type len, flag_type f, const Allocator& a = Allocator()); 
   reg_expression(const reg_expression&); 
   template <class ST, class SA> 
   explicit reg_expression(const std::basic_string<charT, ST, SA>& p, flag_type f = regbase::normal, const Allocator& a = Allocator()); 
   template <class I> 
   reg_expression(I first, I last, flag_type f = regbase::normal, const Allocator& a = Allocator()); 
   ~reg_expression(); 
   reg_expression& operator=(const reg_expression&); 
   reg_expression& operator=(const charT* ptr); 
   template <class ST, class SA> 
   reg_expression& operator=(const std::basic_string<charT, ST, SA>& p); 
   // 
   // assign: 
   reg_expression& assign(const reg_expression& that); 
   reg_expression& assign(const charT* ptr, flag_type f = regbase::normal); 
   reg_expression& assign(const charT* first, const charT* last, flag_type f = regbase::normal); 
   template <class string_traits, class A> 
   reg_expression& assign( 
       const std::basic_string<charT, string_traits, A>& s, 
       flag_type f = regbase::normal); 
   template <class iterator> 
   reg_expression& assign(iterator first, 
                          iterator last, 
                          flag_type f = regbase::normal); 
   // 
   // allocator access: 
   Allocator get_allocator()const; 
   // 
   // locale: 
   locale_type imbue(locale_type l); 
   locale_type getloc()const; 
   // 
   // flags: 
   flag_type getflags()const; 
   // 
   // str: 
   std::basic_string<charT> str()const; 
   // 
   // begin, end: 
   const_iterator begin()const; 
   const_iterator end()const; 
   // 
   // swap: 
   void swap(reg_expression&)throw(); 
   // 
   // size: 
   size_type size()const; 
   // 
   // max_size: 
   size_type max_size()const; 
   // 
   // empty: 
   bool empty()const; 
   unsigned mark_count()const; 
   bool operator==(const reg_expression&)const; 
   bool operator<(const reg_expression&)const; 
};
} // namespace boost 

reg_expression ¥¯¥é¥¹¤Ï¼¡¤Î¤è¤¦¤Ê¸ø³«¥á¥ó¥Ð´Ø¿ô¤ò»ý¤Ã¤Æ¤¤¤ë:


 

  reg_expression(Allocator a = Allocator());  ²¿¤Îɽ¸½¤â¤â¤Ã¤Æ¤¤¤Ê¤¤¡¢ reg_expression ¤Î¥Ç¥Õ¥©¥ë¥È¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤¹¤ë¡£  
  reg_expression(charT* p, unsigned f = regbase::normal, Allocator a = Allocator());  Àµµ¬É½¸½¹½Ê¸¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Õ¥é¥° f ¤ò»È¤Ã¤Æ¡¢ ¥Ì¥ë½ªÃ¼Ê¸»úÎó p ¤Çɽ¤µ¤ì¤¿É½¸½¤«¤é reg_expression ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤¹¤ë¡£ ÍøÍѲÄǽ¤Ê¥Õ¥é¥°¤ÎÃͤ˴ؤ·¤Æ¤Ïregbase ¤ò¸«¤è¡£  
  reg_expression(charT* p1, charT* p2, unsigned f = regbase::normal, Allocator a = Allocator());  Àµµ¬É½¸½¹½Ê¸¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Õ¥é¥° f ¤ò»È¤Ã¤Æ¡¢ ÆþÎÏ¥¤¥Æ¥ì¡¼¥¿ p1 ¤È p2 ¤Çɽ¤µ¤ì¤¿É½¸½¤«¤é reg_expression ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤¹¤ë¡£ ÍøÍѲÄǽ¤Ê¥Õ¥é¥°¤ÎÃͤ˴ؤ·¤Æ¤Ïregbase ¤ò¸«¤è¡£  
  reg_expression(charT* p, size_type len, unsigned f, Allocator a = Allocator());  Àµµ¬É½¸½¹½Ê¸¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Õ¥é¥° f ¤ò»È¤Ã¤Æ¡¢ Ťµ len ¤Îʸ»úÎó p ¤Ë¤è¤Ã¤ÆÉ½¤µ¤ì¤¿É½¸½¤«¤é reg_expression ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤¹¤ë¡£ ÍøÍѲÄǽ¤Ê¥Õ¥é¥°¤ÎÃͤ˴ؤ·¤Æ¤Ïregbase ¤ò¸«¤è¡£  
  template <class ST, class SA>
reg_expression(const std::basic_string<charT, ST, SA>& p, boost::int_fast32_t f = regbase::normal, const Allocator& a = Allocator());
 Àµµ¬É½¸½¹½Ê¸¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Õ¥é¥° f ¤ò»È¤Ã¤Æ¡¢ ʸ»úÎó p ¤Ë¤è¤Ã¤ÆÉ½¤µ¤ì¤¿É½¸½¤«¤é reg_expression ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤¹¤ë¡£ ÍøÍѲÄǽ¤Ê¥Õ¥é¥°¤ÎÃͤ˴ؤ·¤Æ¤Ïregbase ¤ò¸«¤è¡£

Ãí°Õ - ¤³¤Î¥á¥ó¥Ð¤Ï¥³¥ó¥Ñ¥¤¥é¤Ë¤è¤Ã¤Æ¤ÏÍøÍѤǤ­¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¡£

 
  template <class I>
reg_expression(I first, I last, flag_type f = regbase::normal, const Allocator& a = Allocator());
 Àµµ¬É½¸½¹½Ê¸¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Õ¥é¥° f ¤ò»È¤Ã¤Æ¡¢ ÆþÎÏ¥¤¥Æ¥ì¡¼¥¿ p1 ¤È p2 ¤Çɽ¤µ¤ì¤¿É½¸½¤«¤é reg_expression ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤¹¤ë¡£ ÍøÍѲÄǽ¤Ê¥Õ¥é¥°¤ÎÃͤ˴ؤ·¤Æ¤Ïregbase ¤ò¸«¤è¡£    
  reg_expression(const reg_expression&); ¥³¥Ô¡¼¥³¥ó¥¹¥È¥é¥¯¥¿¡£Â¸ºß¤¹¤ëÀµµ¬É½¸½¤ò¥³¥Ô¡¼¤¹¤ë¡£  
  reg_expression& operator=(const reg_expression&); ¸ºß¤¹¤ëÀµµ¬É½¸½¤ò¥³¥Ô¡¼¤¹¤ë¡£  
  reg_expression& operator=(const charT* ptr); assign(ptr) ¤ÈÅù²Á¡£  
  template <class ST, class SA>

reg_expression& operator=(const std::basic_string<charT, ST, SA>& p);

assign(p) ¤ÈÅù²Á¡£  
  reg_expression& assign(const reg_expression& that); that ¤¬»ý¤Ã¤Æ¤¤¤ëÀµµ¬É½¸½¤ò¥³¥Ô¡¼¤¹¤ë¡£ ¤â¤· that ¤¬Í­¸ú¤ÊÀµµ¬É½¸½¤ò»ý¤Ã¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢ bad_expression ¤¬È¯À¸¤¹¤ë¡£ this ¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£  
  reg_expression& assign(const charT* p, flag_type f = regbase::normal); Àµµ¬É½¸½¹½Ê¸¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Õ¥é¥° f ¤ò»È¤Ã¤Æ¡¢ ¥Ì¥ë½ªÃ¼Ê¸»úÎó p ¤Çɽ¤µ¤ì¤¿É½¸½¤«¤éÀµµ¬É½¸½¤ò¥³¥ó¥Ñ¥¤¥ë¤¹¤ë¡£ ÍøÍѲÄǽ¤Ê¥Õ¥é¥°¤ÎÃͤ˴ؤ·¤Æ¤Ïregbase ¤ò¸«¤è¡£ ¤â¤· p ¤¬Í­¸ú¤ÊÀµµ¬É½¸½¤ò»ý¤Ã¤Æ¤¤¤Ê¤±¤ì¤Ð bad_expression ¤¬È¯À¸¤¹¤ë¡£ this ¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£  
  reg_expression& assign(const charT* first, const charT* last, flag_type f = regbase::normal); Àµµ¬É½¸½¹½Ê¸¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Õ¥é¥° f ¤ò»È¤Ã¤Æ¡¢ ÆþÎÏ¥¤¥Æ¥ì¡¼¥¿ first-last ¤Çɽ¤µ¤ì¤¿É½¸½¤«¤éÀµµ¬É½¸½¤ò¥³¥ó¥Ñ¥¤¥ë¤¹¤ë¡£ ÍøÍѲÄǽ¤Ê¥Õ¥é¥°¤ÎÃͤ˴ؤ·¤Æ¤Ïregbase ¤ò¸«¤è¡£ ¤â¤· p ¤¬Í­¸ú¤ÊÀµµ¬É½¸½¤ò»ý¤Ã¤Æ¤¤¤Ê¤±¤ì¤Ð bad_expression ¤¬È¯À¸¤¹¤ë¡£ this ¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£  
  template <class string_traits, class A>
reg_expression& assign(const std::basic_string<charT, string_traits, A>& s, flag_type f = regbase::normal);
Àµµ¬É½¸½¹½Ê¸¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Õ¥é¥° f ¤ò»È¤Ã¤Æ¡¢ ʸ»úÎó s ¤Çɽ¤µ¤ì¤¿É½¸½¤«¤éÀµµ¬É½¸½¤ò¥³¥ó¥Ñ¥¤¥ë¤¹¤ë¡£ ÍøÍѲÄǽ¤Ê¥Õ¥é¥°¤ÎÃͤ˴ؤ·¤Æ¤Ïregbase ¤ò¸«¤è¡£ ¤â¤· p ¤¬Í­¸ú¤ÊÀµµ¬É½¸½¤ò»ý¤Ã¤Æ¤¤¤Ê¤±¤ì¤Ð bad_expression ¤¬È¯À¸¤¹¤ë¡£ this ¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£  
  template <class iterator>
reg_expression& assign(iterator first, iterator last, flag_type f = regbase::normal);
Àµµ¬É½¸½¹½Ê¸¤ò·èÄꤹ¤ë¤¿¤á¤Î¥Õ¥é¥° f ¤ò»È¤Ã¤Æ¡¢ ÆþÎÏ¥¤¥Æ¥ì¡¼¥¿ first-last ¤Çɽ¤µ¤ì¤¿É½¸½¤«¤éÀµµ¬É½¸½¤ò¥³¥ó¥Ñ¥¤¥ë¤¹¤ë¡£ ÍøÍѲÄǽ¤Ê¥Õ¥é¥°¤ÎÃͤ˴ؤ·¤Æ¤Ïregbase ¤ò¸«¤è¡£ ¤â¤· p ¤¬Í­¸ú¤ÊÀµµ¬É½¸½¤ò»ý¤Ã¤Æ¤¤¤Ê¤±¤ì¤Ð bad_expression ¤¬È¯À¸¤¹¤ë¡£ this ¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£  
  Allocator get_allocator()const; Àµµ¬É½¸½¤Ç»È¤ï¤ì¤ë¥¢¥í¥±¡¼¥¿¤òÊÖ¤¹¡£  
  locale_type imbue(const locale_type& l); Àµµ¬É½¸½¤òÆÃÄê¤Î¥í¥±¡¼¥ë¤ËÊѹ¹¤·¡¢ ¸½ºß¤Îɽ¸½¤ò̵¸ú¤Ë¤¹¤ë¡£ ¤â¤·¸Æ¤Ó½Ð¤·¤Î·ë²Ì¡¢¼Âºß¤·¤Ê¤¤¥á¥Ã¥»¡¼¥¸¥«¥¿¥í¥°¤¬³«¤«¤ì¤è¤¦¤È¤·¤¿¤é¡¢ std::runtime_error ¤¬È¯À¸¤¹¤ë¤À¤í¤¦¡£  
  locale_type getloc()const; ɽ¸½¤Ç»È¤ï¤ì¤Æ¤¤¤ë¥í¥±¡¼¥ë¤òÊÖ¤¹¡£  
  flag_type getflags()const; ¸½ºß¤Îɽ¸½¤ò¥³¥ó¥Ñ¥¤¥ë¤¹¤ë¤Î¤Ë»È¤ï¤ì¤Æ¤¤¤ë¥Õ¥é¥°¤òÊÖ¤¹¡£  
  std::basic_string<charT> str()const; ¸½ºß¤Îɽ¸½¤òʸ»úÎó¤È¤·¤ÆÊÖ¤¹¡£  
  const_iterator begin()const; ¸½ºß¤Îɽ¸½¤ÎºÇ½é¤Îʸ»ú¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£  
  const_iterator end()const; ¸½ºß¤Îɽ¸½¤ÎºÇ¸å¤Îʸ»ú¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£  
  size_type size()const; ¸½ºß¤Îɽ¸½¤ÎŤµ¤òÊÖ¤¹¡£  
  size_type max_size()const; Àµµ¬É½¸½Ê¸»úÎó¤ÎºÇÂçŤòÊÖ¤¹¡£  
  bool empty()const; ¥ª¥Ö¥¸¥§¥¯¥È¤¬Í­¸ú¤Êɽ¸½¤ò»ý¤Ã¤Æ¤¤¤Ê¤±¤ì¤Ð true ¤òÊÖ¤¹¡£  
  unsigned mark_count()const ; ¥³¥ó¥Ñ¥¤¥ë¤µ¤ì¤¿Àµµ¬É½¸½¤ÎÃæ¤Î»Òɽ¸½¤Î¿ô¤òÊÖ¤¹ ¤³¤ì¤ÏÁ´ÂΤΰìÃ×(»Òɽ¸½¤¬¥¼¥í)¤â´Þ¤ß¡¢¾ï¤Ë1°Ê¾å¤òÊÖ¤¹¤³¤È¤ËÃí°Õ¤»¤è¡£  

Class regex_traits

#include <boost/regex/regex_traits.hpp>

¤³¤ì¤ÏÀµµ¬É½¸½ÆÃÀ­¥¯¥é¥¹¤ÎͽÈ÷Ū¤Ê¥Ð¡¼¥¸¥ç¥ó¤Ç¤¢¤ê¡¢Êѹ¹¤òɬÍפȤ¹¤ë¡£

ÆÃÀ­¥¯¥é¥¹¤ÎÌÜŪ¤Ï¡¢ reg_expression ¥¯¥é¥¹¤È¡¢¤½¤ì¤Ë´ØÏ¢¤¹¤ë ¥Þ¥Ã¥Á¥ó¥°¥¢¥ë¥´¥ê¥º¥à¤Î¿¶¤ëÉñ¤¤¤Î¥«¥¹¥¿¥Þ¥¤¥º¤ò¤è¤ê´Êñ¤Ë¤¹¤ë¤³¤È¤Ç¤¢¤ë¡£ ¥«¥¹¥¿¥àÆÃÀ­¥¯¥é¥¹¤ÏÆÃ¼ìʸ»ú¥»¥Ã¥È¤ò°·¤¦¤³¤È¤¬½ÐÍè¤Æ¡¢ÄɲäÎʸ»ú¥¯¥é¥¹¤òÄêµÁ¤¹¤ë¡£ Î㤨¤Ð¡¢Á´¤Æ¤Î(¥æ¥Ë¥³¡¼¥É)´Á»úʸ»ú¤È¤·¤Æ [[:kanji:]] ¤òÄêµÁ¤¹¤ë¤³¤È¤¬½ÐÍè¤ë¡£ ¤³¤Î¥é¥¤¥Ö¥é¥ê¤Ï3¤Ä¤ÎÆÃÀ­¥¯¥é¥¹¤È¡¢»È¤ï¤ì¤Æ¤¤¤ë¥Ç¥Õ¥©¥ë¥È¤Î¥í¥±¡¼¥ë¥â¥Ç¥ë¤Ë°Í¸¤·¤Æ ¤³¤Î3¤Ä¤Î¤¦¤Á¤Î¤Ò¤È¤Ä¤ò·Ñ¾µ¤¹¤ë¥é¥Ã¥Ñ¥¯¥é¥¹ regex_traits ¤òÄ󶡤·¤Æ¤¤¤ë¡£ c_regex_traits ¥¯¥é¥¹¤Ï C ¤Î¥°¥í¡¼¥Ð¥ë¥í¥±¡¼¥ë¤ò¥«¥×¥»¥ë²½¤·¤Æ¤¤¤ë¡£ w32_regex_traits ¥¯¥é¥¹¤Ï Win32 ¥°¥í¡¼¥Ð¥ë¥í¥±¡¼¥ë¤ò¥«¥×¥»¥ë²½¤·¤Æ¤¤¤ë (Win32 ¥·¥¹¥Æ¥à¾å¤À¤±¤ÇÍøÍѤǤ­¤ë)¡£ cpp_regex_traits ¤Ï C++ ¤Î¥í¥±¡¼¥ë¤ò¥«¥×¥»¥ë²½¤·¤Æ¤¤¤ë (std::locale ¤¬¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¤È¤­¤Î¤ßÄ󶡤µ¤ì¤ë):

template <class charT> class c_regex_traits;
template<> class c_regex_traits<char> { /*details*/ };
template<> class c_regex_traits<wchar_t> { /*details*/ };

template <class charT> class w32_regex_traits;
template<> class w32_regex_traits<char> { /*details*/ };
template<> class w32_regex_traits<wchar_t> { /*details*/ };

template <class charT> class cpp_regex_traits;
template<> class cpp_regex_traits<char> { /*details*/ };
template<> class cpp_regex_traits<wchar_t> { /*details*/ };

template <class charT> class regex_traits : public base_type { /*detailts*/ };

"base_type" ¤Ï Win32 ¥·¥¹¥Æ¥à¾å¤Ç¤Ï¡¢ ¥Ç¥Õ¥©¥ë¥È¤Ç w32_regex_traits ¤Ç¤¢¤ë¡£¤½¤¦¤Ç¤Ê¤±¤ì¤Ð¡¢ c_regex_traits ¤¬¥Ç¥Õ¥©¥ë¥È¤Ç¤¢¤ë¡£ ¥Ç¥Õ¥©¥ë¥È¤Î¿¶¤ëÉñ¤¤¤Ï BOOST_REGEX_USE_C_LOCALE (c_regex_traits ¤ò¥Ç¥Õ¥©¥ë¥È¤Ç»È¤¦¤è¤¦¤Ë¶¯À©¤¹¤ë)¡¢¤Þ¤¿¤Ï BOOST_REGEX_USE_CPP_LOCALE (cpp_regex_traits ¤ò¥Ç¥Õ¥©¥ë¥È¤Ç»È¤¦¤è¤¦¤Ë¶¯À©¤¹¤ë) ¤òÄêµÁ¤¹¤ë¤³¤È¤Ë¤è¤Ã¤ÆÊѹ¹¤Ç¤­¤ë ¤â¤¦°ì¤Ä¤ÎÁªÂò»è¤È¤·¤Æ¡¢ÆÃÄê¤ÎÆÃÀ­¥¯¥é¥¹¤ò reg_expression ¥Æ¥ó¥×¥ì¡¼¥È¤Ë ÅϤ¹¤³¤È¤â½ÐÍè¤ë¡£

¥«¥¹¥¿¥à¤ÎÀ©Ì󥯥饹¤Î¤¿¤á¤ÎÍ×µá¤Ï ¤³¤³¤Ëµ­¤µ¤ì¤Æ¤¤¤ë¡£

¤Þ¤¿¡¢¥«¥¹¥¿¥à¤ÎÀ©Ì󥯥饹¤ÎÎ㤬 Christian Engströí ¤Ë¤è¤Ã¤ÆÄ󶡤µ¤ì¤Æ¤¤¤ë¡£ iso8859_1_regex_traits.cpp µÚ¤Ó iso8859_1_regex_traits.hpp ¤ò¸«¤è¡£ ¤è¤ê¾ÜºÙ¤Ïreadme file ¤ò¸«¤è¡£


Class match_results

#include <boost/regex.hpp>

Àµµ¬É½¸½¤Ï¡¢Â¿¤¯¤Îñ½ã¤Ê¥Ñ¥¿¡¼¥ó¥Þ¥Ã¥Á¥ó¥°¤Î¥¢¥ë¥´¥ê¥º¥à¤È¤Ï¤³¤È¤Ê¤ë¡£ ¤Ê¤¼¤Ê¤éÁ´ÂΤǤΰìÃפòȯ¸«¤·¡¢»Òɽ¸½¤Î°ìÃפòºî¤ë¤³¤È¤â½ÐÍè¤ë¤«¤é¤Ç¤¢¤ë: ¤½¤ì¤¾¤ì¤Î»Òɽ¸½¤Ï¡¢¥Ñ¥¿¡¼¥ó¤ÎÃæ¤Ç¡¢´Ý³ç¸Ì (...) ¤ÎÁȤˤè¤ê¶èÀÚ¤é¤ì¤Æ¤¤¤ë¡£ »Òɽ¸½¤Î°ìÃפò¥æ¡¼¥¶¤ËÊó¹ð¤¹¤ë¤¿¤á¤ÎÊýË¡¤â¤¢¤ë: ¤³¤ì¤Ï¡¢ »Òɽ¸½¤Î°ìÃפν¸¹ç¤Îº÷°ú¤È¤·¤Æ¤Îµ¡Ç½¤ò¤â¤Ä match_results ¥¯¥é¥¹ ¤Ë¤è¤ê¼Â¸½¤µ¤ì¤Æ¤¤¤ë¡£ ¤½¤ì¤¾¤ì¤Î»Òɽ¸½¤Î°ìÃ×¤Ï sub_match ·¿¤Î¥ª¥Ö¥¸¥§¥¯¥È¤ÎÃæ¤Ë³ÊǼ¤µ¤ì¤Æ¤¤¤ë¡£

// 
// class sub_match: 
// denotes one sub-expression match. 
//         
template <class iterator>
struct sub_match
{
   typedef typename std::iterator_traits<iterator>::value_type       value_type;
   typedef typename std::iterator_traits<iterator>::difference_type  difference_type;
   typedef iterator                                                  iterator_type;
   
   iterator first;
   iterator second;
   bool matched;

   operator std::basic_string<value_type>()const;

   bool operator==(const sub_match& that)const;
   bool operator !=(const sub_match& that)const;
   difference_type length()const;
};

// 
// class match_results: 
// contains an indexed collection of matched sub-expressions. 
// 
template <class iterator, class Allocator = std::allocator<typename std::iterator_traits<iterator>::value_type > > 
class match_results 
{ 
public: 
   typedef Allocator                                                 alloc_type; 
   typedef typename Allocator::template Rebind<iterator>::size_type  size_type; 
   typedef typename std::iterator_traits<iterator>::value_type       char_type; 
   typedef sub_match<iterator>                                       value_type; 
   typedef typename std::iterator_traits<iterator>::difference_type  difference_type; 
   typedef iterator                                                  iterator_type; 
   explicit match_results(const Allocator& a = Allocator()); 
   match_results(const match_results& m); 
   match_results& operator=(const match_results& m); 
   ~match_results(); 
   size_type size()const; 
   const sub_match<iterator>& operator[](int n) const; 
   Allocator allocator()const; 
   difference_type length(int sub = 0)const; 
   difference_type position(unsigned int sub = 0)const; 
   unsigned int line()const; 
   iterator line_start()const; 
   std::basic_string<char_type> str(int sub = 0)const; 
   void swap(match_results& that); 
   bool operator==(const match_results& that)const; 
   bool operator<(const match_results& that)const; 
};
typedef match_results<const char*> cmatch;
typedef match_results<const wchar_t*> wcmatch; 
typedef match_results<std::string::const_iterator> smatch;
typedef match_results<std::wstring::const_iterator> wsmatch; 

match_results ¥¯¥é¥¹¤ÏÀµµ¬É½¸½¤Ë°ìÃפ·¤¿¤â¤Î¤òÊó¹ð¤¹¤ë¤Î¤Ë»È¤ï¤ì¤ë¡£ ¤³¤ì¤Ï¥Þ¥Ã¥Á¥ó¥°¥¢¥ë¥´¥ê¥º¥à regex_match ¤È regex_search ¤òÅϤµ¤ì¤Æ¡¢ regex_grep ¤Ë¤è¤Ã¤Æ¥³¡¼¥ë¥Ð¥Ã¥¯´Ø¿ô(¤Þ¤¿¤Ï´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È) ¤Ë¤Ë²¿¤¬°ìÃפ·¤¿¤Î¤«¤òÃΤ餻¤ë¡£ °ìÃ×ȽÄê¤Î°Ù¤ËÁª¤Ð¤ì¤ë¥Ç¥Õ¥©¥ë¥È¤Î¥¢¥í¥±¡¼¥¿¥Ñ¥é¥á¡¼¥¿¤Ï¡¢ reg_expresion ¤Î ¥Ç¥Õ¥©¥ë¥È¤Î¥¢¥í¥±¡¼¥¿¥Ñ¥é¥á¡¼¥¿¤Ç¤¢¤ë¤³¤È¤ËÃí°Õ¤»¤è¡£ match_results ¤Ë¤Ï¼¡¤Î¸ø³«¥á¥ó¥Ð´Ø¿ô¤¬¤¢¤ë:
 

  match_results(Allocator a = Allocator()); ¥¢¥í¥±¡¼¥¿¤Î¥¤¥ó¥¹¥¿¥ó¥¹ a ¤ò»È¤Ã¤Æ¡¢ match_results ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ò¹½ÃÛ¤¹¤ë¡£  
  match_results(const match_results& m); ¥³¥Ô¡¼¥³¥ó¥¹¥È¥é¥¯¥¿  
  match_results& operator=(const match_results& m); ÂåÆþ±é»»»Ò¡£  
  const sub_match<iterator>& operator[](size_type n) const; °ìÃפ·¤¿¤â¤Î¤òÊÖ¤¹¡£ n ¤¬ 0 ¤Ê¤éʸ»úÎóÁ´ÂΤò¡¢ n ¤¬ 1 ¤Ê¤éºÇ½é¤Î»Òɽ¸½¤òɽ¤·¤Æ¤¤¤ë¡£ (ÌõÃí: °Ê²¼¡¢¤½¤Î¿ô»ú¤ËÂбþ¤¹¤ë»Òɽ¸½¤òɽ¤·¤Æ¤¤¤ë)  
  Allocator& allocator()const; ¤³¤Î¥¯¥é¥¹¤Ç»È¤ï¤ì¤Æ¤¤¤ë¥¢¥í¥±¡¼¥¿¤òÊÖ¤¹¡£  
  difference_type length(unsigned int sub = 0); °ìÃפ·¤¿»Òɽ¸½¤ÎŤµ¤òÊÖ¤¹¡£ ¥Ç¥Õ¥©¥ë¥È¤Ç¤ÏÁ´ÂΤΰìÃפÎŤµ¤òÊÖ¤¹¡£ Íפ¹¤ë¤Ë¤³¤ì¤Ï¡¢operator [](sub).second - operator[](sub).first ¤ÈÅù²Á¤Ç¤¢¤ë¡£  
  difference_type position(unsigned int sub = 0); °ìÃפ·¤¿»Òɽ¸½¤Î°ÌÃÖ¤òÊÖ¤¹¡£ ¥Ç¥Õ¥©¥ë¥È¤Ç¤ÏÁ´ÂΤΰìÃפΰÌÃÖ¤òÊÖ¤¹¡£ Ìá¤êÃͤϡ¢Ê¸»úÎó¤ÎÀèÆ¬¤«¤é¤ÎÁêÂÐŪ¤Ê°ìÃפΰÌÃ֤Ǥ¢¤ë¡£  
  unsigned int line()const; °ìÃפ¬µ¯¤³¤Ã¤¿¹ÔÈÖ¹æ¤òÊÖ¤¹¡£ ¹ÔÈÖ¹æ¤ÎÀèÆ¬¤Ï 0 ¤Ç¤Ê¤¯ 1 ¤Ç¤¢¤ë¡£ operator[](0) ¤è¤êÁ°¤Î²þ¹Ôʸ»ú¤Î¿ô¤Ë 1 ¤ò²Ã¤¨¤¿¤â¤Î¤ÈÅù²Á¤Ç¤¢¤ë¡£  
  iterator line_start()const; °ìÃפ¬µ¯¤³¤Ã¤¿¹Ô¤ÎÀèÆ¬¤ò¼¨¤¹¥¤¥Æ¥ì¡¼¥¿¤òÊÖ¤¹¡£  
  size_type size()const; °ìÃפÎÃæ¤Ë¡¢ »Òɽ¸½ 0 (Á´ÂΤΰìÃ×) ¤â´Þ¤á¤Æ¡¢ ¤¤¤¯¤Ä¤Î»Òɽ¸½¤¬Â¸ºß¤¹¤ë¤«¤òÊÖ¤¹¡£ ¤â¤·¸¡º÷¤ÎÁàºî¤Ç°ìÃפ¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¤È¤­¡¢¤³¤ì¤Ï½ÅÍפǤ¢¤ë¡£ °ìÃפ¬µ¯¤³¤Ã¤¿¤«¤É¤¦¤«·èÄꤹ¤ë¤Î¤Ë¡¢ regex_search / regex_match ¤«¤é¤ÎÌá¤êÃͤò»È¤ï¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£  


 

¥á¥ó¥Ð´Ø¿ô operator[] ¤Ï¹¹¤ËÀâÌÀ¤¬É¬ÍפǤ¢¤ë¡£¤³¤ì¤Ï sub_match<iterator> ·¿ ¤Î¹½Â¤ÂΤؤΠconst ¤Î»²¾È¤òÊÖ¤¹¡£ sub_match<iterator> ¤Ï¼¡¤Î¸ø³«¥á¥ó¥Ð¤ò»ý¤Ã¤Æ¤¤¤ë:
 

  typedef typename std::iterator_traits<iterator>::value_type value_type; ¥¤¥Æ¥ì¡¼¥¿¤Ë¤è¤Ã¤Æ»Ø¤µ¤ì¤Æ¤¤¤ë·¿¡£  
  typedef typename std::iterator_traits<iterator>::difference_type difference_type; Æó¤Ä¤Î¥¤¥Æ¥ì¡¼¥¿¤Î°ã¤¤¤òɽ¤¹·¿¡£  
  typedef iterator iterator_type; ¥¤¥Æ¥ì¡¼¥¿¤Î·¿¡£  
  iterator first °ìÃפÎÀèÆ¬¤Î°ÌÃÖ¤ò¼¨¤¹¥¤¥Æ¥ì¡¼¥¿¡£  
  iterator second °ìÃפνªÃ¼¤Î°ÌÃÖ¤ò¼¨¤¹¥¤¥Æ¥ì¡¼¥¿¡£  
  bool matched ¤³¤Î»Òɽ¸½¤¬°ìÃפ·¤¿¤â¤Î¤ÎÃæ¤Ë¤¢¤ë¤«¤ò¼¨¤¹ bool ÃÍ¡£  
  difference_type length()const; »Òɽ¸½¤Î°ìÃפÎŤµ¤òÊÖ¤¹¡£  
  operator std::basic_string<value_type> ()const; »Òɽ¸½¤Î°ìÃפò std::basic_string<> ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤ËÊÑ´¹¤¹¤ë¡£ ¤³¤Î¥á¥ó¥Ð¤Ï¸ºß¤·¤Ê¤¤¤«¡¢¥³¥ó¥Ñ¥¤¥é¤Ë°Í¸¤·¤Æ¡¢¤è¤ê¸Â¤é¤ì¤ÆÂ¸ºß¤¹¤ë¤«¤Î¤É¤Á¤é¤«¤À¤í¤¦¡¢ ¤È¤¤¤¦¤³¤È¤ËÃí°Õ¤»¤è¡£  

operator [] ¤Ï¾ðÊó¤òÊÖ¤¹»Òɽ¸½¤òɽ¤¹°ú¿ô¤È¤·¤ÆÀ°¿ôÃͤò¼è¤ë¡£ °ú¿ô¤Ï¼¡¤Î¤è¤¦¤ÊÆÃ¼ì¤ÊÃͤò¼è¤ê¤¦¤ë:
 

  -2 °ìÃפνªÃ¼¤«¤é¡¢ÆþÎϤµ¤ì¤¿Ê¸»úÎó¤Î½ªÃ¼¤Þ¤Ç¤Î Á´¤Æ¤òÊÖ¤¹¡£¤³¤ì¤Ï perl ¤Ç¤Î $' ¤ÈÅù²Á¤Ç¤¢¤ë¡£ ¤â¤·¤³¤ì¤¬ null ʸ»úÎó¤Ê¤é:

first == second

¤«¤Ä

matched == false

¤Ç¤¢¤ë¡£

 
  -1 ÆþÎϤµ¤ì¤¿Ê¸»úÎó¤ÎÀèÆ¬¤«¤é (¤Þ¤¿¤Ï¡¢¤â¤· grep ¤ÎÁàºî¤Ç¤¢¤ì¤ÐºÇ¸å¤Î°ìÃפνªÃ¼¤«¤é) ¤³¤Î°ìÃפÎÀèÆ¬¤Þ¤Ç¤ÎÁ´¤Æ¤òÊÖ¤¹¡£ perl ¤Ç¤Î $` ¤ÈÅù²Á¤Ç¤¢¤ë¡£ ¤â¤·¤³¤ì¤¬ null ʸ»úÎó¤Ê¤é:

first == second

¤«¤Ä

matched == false.

¤Ç¤¢¤ë¡£

 
  0 °ìÃפ·¤¿¤â¤ÎÁ´ÂΤòÊÖ¤¹¡£ perl ¤Ç¤Î $& ¤ÈÅù²Á¤Ç¤¢¤ë¡£ ¥Ñ¥é¥á¡¼¥¿ matched ¤Ï¾ï¤Ë true ¤Ç¤¢¤ë¡£  
  0 < N < size() »Òɽ¸½ N ¤Ë°ìÃפ·¤¿¤â¤Î¤òÊÖ¤¹¡£ ¤â¤·¤³¤Î»Òɽ¸½¤¬²¿¤â°ìÃפ·¤Æ¤¤¤Ê¤«¤Ã¤¿¤é¡¢

matched == false

¤½¤¦¤Ç¤Ê¤±¤ì¤Ð

matched == true

¤Ç¤¢¤ë¡£

 
  N < -2 or N >= size() Èϰϳ°¤Î¸ºß¤·¤Ê¤¤»Òɽ¸½¤òɽ¤·¤Æ¤¤¤ë¡£ ¡Ö¶õ¤Î¡×°ìÃפòÊÖ¤¹¡£¤Ä¤Þ¤ê¡¢

first == last

¤«¤Ä

matched == false.

¤Ç¤¢¤ë¡£

 

¥¢¥í¥±¡¼¥¿¤Î¥Ñ¥é¥á¡¼¥¿¤ÈƱÍͤˡ¢ match_results<> ¤â¤Þ¤¿ ¥¤¥Æ¥ì¡¼¥¿·¿¤ò¤â¤Á¡¢¤³¤ì¤Ï¤É¤ó¤Ê¥¤¥Æ¥ì¡¼¥¿¤ÎÁȤ߹ç¤ï¤»¤â¡¢¤½¤ì¤¬ ÁÐÊý¸þ¥¤¥Æ¥ì¡¼¥¿¤Ç¤¢¤ë¤Ê¤é¡¢ Í¿¤¨¤é¤ì¤¿Àµµ¬É½¸½¤Ç¸¡º÷¤¹¤ë¤³¤È¤¬½ÐÍè¤ë¡¢¤È¤¤¤¦¤³¤È¤ËÃí°Õ¤»¤è¡£


Algorithm regex_match

#include <boost/regex.hpp>

¥¢¥ë¥´¥ê¥º¥à regex_match ¤ÏÍ¿¤¨¤é¤ì¤¿Àµµ¬É½¸½¤¬¡¢ °ìÁȤÎÁÐÊý¸þ¥¤¥Æ¥ì¡¼¥¿¤Ë¤è¤Ã¤ÆÉ½¤µ¤ì¤ë¥·¡¼¥±¥ó¥¹¤Ë°ìÃפ¹¤ë¤«¤ò·èÄꤹ¤ë¡£ ¤³¤Î¥¢¥ë¥´¥ê¥º¥à¤Ï°Ê²¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£ ÆþÎÏ¥·¡¼¥±¥ó¥¹Á´ÂΤȰìÃפ·¤¿¤È¤­¤Î¤ß·ë²Ì¤¬ true ¤Ç¤¢¤ë¤³¤È¤ËÃí°Õ¤»¤è¡£ ¤³¤Î´Ø¿ô¤Î¼çÍפʻȤ¤Æ»¤Ï¡¢ÆþÎϥǡ¼¥¿¤Î¸¡¾Ú¤Ç¤¢¤ë:

template <class iterator, class Allocator, class charT, class traits, class Allocator2>
bool regex_match(iterator first, 
                 iterator last, 
                 match_results<iterator, Allocator>& m, 
                 const reg_expression<charT, traits, Allocator2>& e, 
                 unsigned flags = match_default);

¥é¥¤¥Ö¥é¥ê¤Ï¼¡¤Î´Ê°×ÈǤâÄêµÁ¤·¤Æ¤¤¤ë¡£ ¤³¤ì¤Ï const charT* ·¿¤«¡¢°¿¤¤¤Ï const std::basic_string<>& ·¿¤ò °ìÁȤΥ¤¥Æ¥ì¡¼¥¿¤ÎÂå¤ï¤ê¤Ë¤È¤ë¡£[¥³¥ó¥Ñ¥¤¥é¤Ë¤è¤Ã¤Æ¤Ï¤³¤ì¤é¤Î´Ê°×ÈÇ¤Ï ÍøÍѤǤ­¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¤³¤È¡¢°¿¤¤¤Ï¸Â¤é¤ì¤¿·Á¤Ç¤·¤«ÍøÍѤǤ­¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¤³¤È ¤ËÃí°Õ¤»¤è]:

template <class charT, class Allocator, class traits, class Allocator2>
bool regex_match(const charT* str, 
                 match_results<const charT*, Allocator>& m, 
                 const reg_expression<charT, traits, Allocator2>& e, 
                 unsigned flags = match_default)

template <class ST, class SA, class Allocator, class charT, class traits, class Allocator2>
bool regex_match(const std::basic_string<charT, ST, SA>& s, 
                 match_results<typename std::basic_string<charT, ST, SA>::const_iterator, Allocator>& m, 
                 const reg_expression<charT, traits, Allocator2>& e, 
                 unsigned flags = match_default);

ºÇ¸å¤Ë¡¢¤¿¤À true ¤« false ¤òÊÖ¤¹¤À¤±¤Ç¡¢ ²¿¤¬°ìÃפ·¤¿¤«¤Ë´ØÍ¿¤·¤Ê¤¤´Ê°×ÈǤ¬¤¢¤ë¡£

template <class iterator, class Allocator, class charT, class traits, class Allocator2>
bool regex_match(iterator first, 
                 iterator last, 
                 const reg_expression<charT, traits, Allocator2>& e, 
                 unsigned flags = match_default);

template <class charT, class Allocator, class traits, class Allocator2>
bool regex_match(const charT* str, 
                 const reg_expression<charT, traits, Allocator2>& e, 
                 unsigned flags = match_default)

template <class ST, class SA, class Allocator, class charT, class traits, class Allocator2>
bool regex_match(const std::basic_string<charT, ST, SA>& s, 
                 const reg_expression<charT, traits, Allocator2>& e, 
                 unsigned flags = match_default);

¼çÍ×ÈÇ(ÌõÃí:´Ê°×ÈǤǤϤʤ¤ÄêµÁ)¤Î´Ø¿ô¤Î¥Ñ¥é¥á¡¼¥¿¤Ï¼¡¤ÎÄ̤ê¤Ç¤¢¤ë¡£
 

  iterator first °ìÃפµ¤»¤ëÈϰϤÎÀèÆ¬¤ò¼¨¤¹¡£  
  iterator last °ìÃפµ¤»¤ëÈϰϤνªÃ¼¤ò¼¨¤¹¡£  
  match_results<iterator, Allocator>& m ²¿¤¬°ìÃפ·¤¿¤«¤òÊó¹ð¤¹¤ë¤¿¤á¤Î match_results ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¡£´Ø¿ô¤òÈ´¤±¤ë¤È¤­¤Ë¡¢¤â¤·°ìÃפ¬µ¯¤³¤Ã¤Æ¤¤¤¿¤é m[0] ¤Ï°ìÃפ·¤¿Ê¸»úÎó¤ÎÁ´ÂΤòɽ¤¹¡£ m[0].first ¤Ï first ¤ÈÅù¤·¤¯¡¢ m[0].second ¤Ï last ¤è¤ê¾¯¤Ê¤¤¤«¡¢Åù¤·¤¤¡£ m[1] ¤ÏºÇ½é¤Î»Òɽ¸½¡¢ m[2] ¤Ï2ÈÖÌܤλÒɽ¸½¤ò¼¨¤·¡¢°Ê²¼¤½¤ì¤¬Â³¤¯¡£ ¤â¤·°ìÃפ¬µ¯¤³¤é¤Ê¤±¤ì¤Ð¡¢ m[0].first = m[0].second = last ¤Ç¤¢¤ë¡£

match_results ¹½Â¤ÂΤϥ¤¥Æ¥ì¡¼¥¿¤Î¤ß¤òÊÝ»ý¤·¡¢Ê¸»úÎó¤òÊÝ»ý¤·¤Æ¤¤¤Ê¤¤¤Î¤Ç¡¢ regex_match ¤ËÅϤµ¤ì¤ë¥¤¥Æ¥ì¡¼¥¿¤Èʸ»úÎó¤Ï¡¢¤½¤Î·ë²Ì¤¬»È¤ï¤ì¤ë¸Â¤ê Í­¸ú¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤³¤È¤ËÃí°Õ¤»¤è¡£¤³¤Î¤¿¤á¡¢·è¤·¤Æ°ì»þŪʸ»úÎ󥪥֥¸¥§¥¯¥È¤ò regex_match ¤ËÅϤ·¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£

 
  const reg_expression<charT, traits, Allocator2>& e °ìÃ×ȽÄê¤Ë¤Ä¤«¤¦Àµµ¬É½¸½¤ò»ý¤Ä¡£  
  unsigned flags = match_default °ìÃפ˻Ȥï¤ì¤ë¥»¥Þ¥ó¥Æ¥£¥¯¥¹¤ò·èÄꤹ¤ë¡£ ¤Ò¤È¤Ä°Ê¾å¤Î match_flags Îóµó»Ò¤ÎÁȤ߹ç¤ï¤»¤Ç¤¢¤ë¡£  

regex_match ¤Ï°ìÃפ¬µ¯¤³¤é¤Ê¤±¤ì¤Ð false ¤ò¡¢µ¯¤³¤ì¤Ð true ¤òÊÖ¤¹¡£ °ìÃפϡ¢ first ¤Ç»Ï¤Þ¤ê¡¢ last ¤Ç½ª¤ï¤ë¤È¤­¤Ë¤À¤±µ¯¤³¤ë¡£ Î㤨¤Ð¡¢¼¡¤ÎÎã: example ¤Ï FTP ¥ì¥¹¥Ý¥ó¥¹¤Î½èÍý¤Ç¤¢¤ë¡£

#include <stdlib.h> 
#include <boost/regex.hpp> 
#include <string> 
#include <iostream> 

using namespace boost; 

regex expression("([0-9]+)(\\-| |$)(.*)"); 

// process_ftp: 
// on success returns the ftp response code, and fills 
// msg with the ftp response message. 
int process_ftp(const char* response, std::string* msg) 
{ 
   cmatch what; 
   if(regex_match(response, what, expression)) 
   { 
      // what[0] contains the whole string 
      // what[1] contains the response code 
      // what[2] contains the separator character 
      // what[3] contains the text message. 
      if(msg) 
         msg->assign(what[3].first, what[3].second); 
      return std::atoi(what[1].first); 
   } 
   // failure did not match 
   if(msg) 
      msg->erase(); 
   return -1; 
}

¥¢¥ë¥´¥ê¥º¥à¤ËÅϤµ¤ì¤ë¥Õ¥é¥°¥Ñ¥é¥á¡¼¥¿¤ÎÃÍ¤Ï ¼¡¤ÎÃͤΤҤȤİʾå¤ÎÁȤ߹ç¤ï¤»¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
 

  match_default ¥Ç¥Õ¥©¥ë¥È¤ÎÃͤǤ¢¤ë¡£ first ¤Ï¹Ô¤ÎÀèÆ¬¡¢¥Ð¥Ã¥Õ¥¡¤ÎÀèÆ¬¡¢¤½¤·¤Æ(²Äǽ¤Ê¤é)ñ¸ì¤ÎÀèÆ¬¤òɽ¤¹¡£ last ¤Ï¹Ô¤ÎºÇ¸å¡¢¥Ð¥Ã¥Õ¥¡¤ÎºÇ¸å¡¢¤½¤·¤Æ(²Äǽ¤Ê¤é)ñ¸ì¤ÎºÇ¸å¤òɽ¤¹¡£ »Òɽ¸½¥É¥Ã¥È "."¤Ï²þ¹Ôʸ»ú¤È¥Ì¥ëʸ»ú¤ÎξÊý¤Ë¤â°ìÃפ¹¤ë¡£  
  match_not_bol ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ first ¤Ï¿·¤·¤¤¹Ô¤ÎÀèÆ¬¤òɽ¤µ¤Ê¤¤¡£  
  match_not_eol ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ last ¤Ï¹Ô¤ÎºÇ¸å¤òɽ¤µ¤Ê¤¤¡£  
  match_not_bob ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ first ¤Ï¥Ð¥Ã¥Õ¥¡¤Î»Ï¤Þ¤ê¤òɽ¤µ¤Ê¤¤¡£  
  match_not_eob ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ last ¤Ï¥Ð¥Ã¥Õ¥¡¤Î½ª¤ï¤ê¤òɽ¤µ¤Ê¤¤¡£  
  match_not_bow ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ first ¤Ïñ¸ì¤ÎÀèÆ¬¤Ë°ìÃפ¹¤ë¤³¤È¤¬½ÐÍè¤Ê¤¤¡£  
  match_not_eow ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ last ¤Ïñ¸ì¤Î½ª¤ï¤ê¤Ë°ìÃפ¹¤ë¤³¤È¤¬½ÐÍè¤Ê¤¤¡£  
  match_not_dot_newline ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ ¥É¥Ã¥Èɽ¸½ "." ¤Ï²þ¹Ôʸ»ú¤Ë°ìÃפ·¤Ê¤¤  
  match_not_dot_null ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ ¥É¥Ã¥Èɽ¸½ "." ¤Ï¥Ì¥ëʸ»ú¤Ë°ìÃפ·¤Ê¤¤¡£  
  match_prev_avail ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ *--first ¤ÏÍ­¸ú¤Êɽ¸½¤Ç¤¢¤ê¡¢¤³¤ì¤é¤ò¸¡¾Ú¤¹¤ë¤Î¤ËÁ°¤Îʸ»ú¤ÎÃͤ¬»È¤ï¤ì¤ë¤Î¤Ç match_not_bol ¤È match_not_bow ¤ÏºîÍѤ·¤Ê¤¤¡£  
  match_any ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ ²Äǽ¤Ê¸Â¤êŤ¯°ìÃפ¹¤ë¤Î¤Ç¤Ï¤Ê¤¯¡¢ °ìÃפ·¤¿ºÇ½é¤Îʸ»úÎó¤¬ÊÖ¤µ¤ì¤ë¡£ ¤³¤Î¥Õ¥é¥°¤Ï°ìÃפòȯ¸«¤¹¤ë¤Î¤Ë¤«¤«¤ë»þ´Ö¤òÈó¾ï¤Ëºï¸º¤¹¤ë¤¬¡¢ ²¿¤¬°ìÃפ¹¤ë¤«¤Ï̤ÄêµÁ¤Ç¤¢¤ë¡£  
  match_not_null ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ Àµµ¬É½¸½¤Ï¥Ì¥ëʸ»úÎó¤Ë¤Ï·è¤·¤Æ°ìÃפ·¤Ê¤¤¡£  
  match_continuous ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ grep Áàºî¤Î´Ö¤Ë¡¢°ìÏ¢¤Î°ìÃפϤ½¤ì¤¾¤ì¡¢ °ÊÁ°¤Î°ìÃפ¬½ªÎ»¤·¤¿¾ì½ê¤«¤é»Ï¤Þ¤ë¡£  
  match_partial ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¡¢ Àµµ¬É½¸½¥¢¥ë¥´¥ê¥º¥à¤ÏÉôʬ°ìÃ×: partial matches ¤òÊó¹ð¤¹¤ë¡£ ¤³¤ì¤ÏÆþÎÏʸ»úÎó¤ÎºÇ¸å¤Î¤Ò¤È¤Ä°Ê¾å¤Îʸ»ú¤¬¡¢ Àµµ¬É½¸½¤ÎÀÜÆ¬¼­(ÌõÃí:ÆþÎÏʸ»úÎó¤ÎºÇ¸å¤ÎǤ°Õ¤ÎÉôʬ¤È¡¢ Àµµ¬É½¸½¤ÎÁ°¤«¤é¤¤¤¯¤Ä¤«¤¬°ìÃפ¹¤ë¤È¤¤¤¦¤³¤È) ¤Ë°ìÃפ¹¤ë¤È¤¤¤¦¤³¤È¤Ç¤¢¤ë¡£  

 


Algorithm regex_search

 #include <boost/regex.hpp>

¥¢¥ë¥´¥ê¥º¥à regex_search ¤Ï°ìÁȤÎÁÐÊý¸þ¥¤¥Æ¥ì¡¼¥¿¤Ë¤è¤Ã¤Æ¼¨¤µ¤ì¤ëÈϰϤò¡¢ Í¿¤¨¤é¤ì¤¿Àµµ¬É½¸½¤Ç¸¡º÷¤¹¤ë¡£ ¤³¤Î¥¢¥ë¥´¥ê¥º¥à¤Ï¡¢°ìÃפ¬¤½¤Î°ÌÃ֤ǻϤޤë²ÄǽÀ­¤¬¤¢¤ë¤È¤­¤Î¤ß¡¢ °ìÃפò¸¡¾Ú¤¹¤ë¤³¤È¤Ç¡¢¸¡º÷»þ´Ö¤òºï¸º¤¹¤ë¤¿¤á¤ËÍÍ¡¹¤Êõº÷ŪÊýË¡¤òÍøÍѤ¹¤ë¡£ ¤³¤Î¥¢¥ë¥´¥ê¥º¥à¤Ï¼¡¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£

template <class iterator, class Allocator, class charT, class traits, class Allocator2>
bool regex_search(iterator first, 
                iterator last, 
                match_results<iterator, Allocator>& m, 
                const reg_expression<charT, traits, Allocator2>& e, 
                unsigned flags = match_default);

¥é¥¤¥Ö¥é¥ê¤Ï¼¡¤Î´Ê°×ÈǤâÄêµÁ¤·¤Æ¤¤¤ë¡£ ¤³¤ì¤Ï °ìÁȤΥ¤¥Æ¥ì¡¼¥¿¤ÎÂå¤ï¤ê¤Ë¡¢ const charT* ·¿¤«¡¢°¿¤¤¤Ï const std::basic_string<>& ·¿¤ò¤È¤ë¡£ [¥³¥ó¥Ñ¥¤¥é¤Ë¤è¤Ã¤Æ¤Ï¤³¤ì¤é¤Î´Ê°×ÈǤÏÍøÍѤǤ­¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¤³¤È¡¢ °¿¤¤¤Ï¸Â¤é¤ì¤¿·Á¤Ç¤·¤«ÍøÍѤǤ­¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¤³¤È¤ËÃí°Õ¤»¤è]:

template <class charT, class Allocator, class traits, class Allocator2>
bool regex_search(const charT* str, 
                match_results<const charT*, Allocator>& m, 
                const reg_expression<charT, traits, Allocator2>& e, 
                unsigned flags = match_default);

template <class ST, class SA, class Allocator, class charT, class traits, class Allocator2>
bool regex_search(const std::basic_string<charT, ST, SA>& s, 
                match_results<typename std::basic_string<charT, ST, SA>::const_iterator, Allocator>& m, 
                const reg_expression<charT, traits, Allocator2>& e, 
                unsigned flags = match_default);

¼çÍ×ÈÇ(ÌõÃí:´Ê°×ÈǤǤϤʤ¤ÄêµÁ)¤Î´Ø¿ô¤Î¥Ñ¥é¥á¡¼¥¿¤Ï¼¡¤ÎÄ̤ê¤Ç¤¢¤ë¡£
 

  iterator first ¸¡º÷ÈϰϤγ«»Ï°ÌÃÖ¡£  
  iterator last ¸¡º÷ÈϰϤνªÎ»°ÌÃÖ¡£  
  match_results<iterator, Allocator>& m ²¿¤¬°ìÃפ·¤¿¤«¤òÊó¹ð¤¹¤ë¤¿¤á¤Î match_results ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¡£´Ø¿ô¤òÈ´¤±¤ë¤È¤­¤Ë¡¢¤â¤·°ìÃפ¬µ¯¤³¤Ã¤Æ¤¤¤¿¤é m[0] ¤Ï°ìÃפ·¤¿Ê¸»úÎó¤ÎÁ´ÂΤòɽ¤¹¡£ m[0].first ¤Ï first ¤ÈÅù¤·¤¯¡¢ m[0].second ¤Ï last ¤è¤ê¾¯¤Ê¤¤¤«¡¢Åù¤·¤¤¡£ m[1] ¤ÏºÇ½é¤Î»Òɽ¸½¡¢ m[2] ¤Ï2ÈÖÌܤλÒɽ¸½¤ò¼¨¤·¡¢°Ê²¼¤½¤ì¤¬Â³¤¯¡£ ¤â¤·°ìÃפ¬µ¯¤³¤é¤Ê¤±¤ì¤Ð¡¢ m[0].first = m[0].second = last ¤Ç¤¢¤ë¡£

match_results ¹½Â¤ÂΤϥ¤¥Æ¥ì¡¼¥¿¤Î¤ß¤òÊÝ»ý¤·¡¢Ê¸»úÎó¤òÊÝ»ý¤·¤Æ¤¤¤Ê¤¤¤Î¤Ç¡¢ regex_search ¤ËÅϤµ¤ì¤ë¥¤¥Æ¥ì¡¼¥¿¤Èʸ»úÎó¤Ï¡¢¤½¤Î·ë²Ì¤¬»È¤ï¤ì¤ë¸Â¤ê Í­¸ú¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤³¤È¤ËÃí°Õ¤»¤è¡£¤³¤Î¤¿¤á¡¢·è¤·¤Æ°ì»þŪʸ»úÎ󥪥֥¸¥§¥¯¥È¤ò regex_search ¤ËÅϤ·¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£

 
  const reg_expression<charT, traits, Allocator2>& e ¸¡º÷¤Ë»È¤ï¤ì¤ëÀµµ¬É½¸½¡£  
  unsigned flags = match_default ²¿¤¬°ìÃפ¹¤ë¤«¤ò·èÄꤹ¤ë¥Õ¥é¥°¡£ match_flags Îóµó»Ò¤Î¤Ò¤È¤Ä°Ê¾å¤ÎÁȤ߹ç¤ï¤»¡£  


 

¼¡¤ÎÎã: example ¤Ïʸ»úÎó¤Î·Á¤Ç¥Õ¥¡¥¤¥ë¤ÎÆâÍÆ¤ò¼õ¤±¼è¤ê¡¢¥Õ¥¡¥¤¥ë¤ÎÃæ¤ÎÁ´¤Æ¤Î C++ ¥¯¥é¥¹Àë¸À¤ò¸¡º÷¤¹¤ë¡£ ¥³¡¼¥É¤Ï std::string ¤¬¤É¤Î¤è¤¦¤Ë¼ÂÁõ¤µ¤ì¤Æ¤¤¤è¤¦¤¬Æ°¤¯¡£ Î㤨¤Ð ÉÔϢ³¤ÎÊÝ»ý(ÌõÃí: ¥³¥ó¥Æ¥Ê¤ÎÍ×ÁǤ¬¥á¥â¥ê¾å¤ÇϢ³¤·¤Æ¤¤¤Ê¤¤¤³¤È)¤ò»È¤Ã¤Æ¤¤¤ë SGI rope ¥¯¥é¥¹¤Ç¤³¤ì¤¬Æ°¤¯¤è¤¦¤Ë´Êñ¤ËÊѹ¹¤Ç¤­¤ë¡£

#include <string> 
#include <map> 
#include <boost/regex.hpp> 

// purpose: 
// takes the contents of a file in the form of a string 
// and searches for all the C++ class definitions, storing 
// their locations in a map of strings/int's 
typedef std::map<std::string, int, std::less<std::string> > map_type; 

boost::regex expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?(\\{|:[^;\\{()]*\\{)"); 

void IndexClasses(map_type& m, const std::string& file) 
{ 
   std::string::const_iterator start, end; 
   start = file.begin(); 
   end = file.end(); 
      boost::match_results<std::string::const_iterator> what; 
   unsigned int flags = boost::match_default; 
   while(regex_search(start, end, what, expression, flags)) 
   { 
      // what[0] contains the whole string 
      // what[5] contains the class name. 
      // what[6] contains the template specialisation if any. 
      // add class name and position to map: 
      m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = 
                what[5].first - file.begin(); 
      // update search position: 
      start = what[0].second; 
      // update flags: 
      flags |= boost::match_prev_avail; 
      flags |= boost::match_not_bob; 
   } 
}
 

Algorithm regex_grep

#include <boost/regex.hpp>

 Regex_grep ¤Ï¡¢ÁÐÊý¸þ¥¤¥Æ¥ì¡¼¥¿¤ÎÈϰϤòºÇ½é¤«¤éºÇ¸å¤Þ¤Ç¸¡º÷¤·¡¢ Í¿¤¨¤é¤ì¤¿Àµµ¬É½¸½¤È¤ÎÁ´¤Æ¤Î(½Å¤Ê¤é¤Ê¤¤)°ìÃפòȯ¸«¤¹¤ë¡£ ¤³¤Î´Ø¿ô¤Ï¼¡¤Î¤è¤¦¤ËÀë¸À¤µ¤ì¤Æ¤¤¤ë:

template <class Predicate, class iterator, class charT, class traits, class Allocator>
unsigned int regex_grep(Predicate foo, 
                        iterator first, 
                        iterator last, 
                        const reg_expression<charT, traits, Allocator>& e, 
                        unsigned flags = match_default)

¥é¥¤¥Ö¥é¥ê¤Ï¼¡¤Î´Ê°×ÈǤâÄêµÁ¤·¤Æ¤¤¤ë¡£ ¤³¤ì¤Ï °ìÁȤΥ¤¥Æ¥ì¡¼¥¿¤ÎÂå¤ï¤ê¤Ë¡¢ const charT* ·¿¤«¡¢°¿¤¤¤Ï const std::basic_string<>& ·¿¤ò¤È¤ë¡£ [¥³¥ó¥Ñ¥¤¥é¤Ë¤è¤Ã¤Æ¤Ï¤³¤ì¤é¤Î´Ê°×ÈǤÏÍøÍѤǤ­¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¤³¤È¡¢ °¿¤¤¤Ï¸Â¤é¤ì¤¿·Á¤Ç¤·¤«ÍøÍѤǤ­¤Ê¤¤¤«¤â¤·¤ì¤Ê¤¤¤³¤È¤ËÃí°Õ¤»¤è]:

template <class Predicate, class charT, class Allocator, class traits>
unsigned int regex_grep(Predicate foo, 
              const charT* str, 
              const reg_expression<charT, traits, Allocator>& e, 
              unsigned flags = match_default);

template <class Predicate, class ST, class SA, class Allocator, class charT, class traits>
unsigned int regex_grep(Predicate foo, 
              const std::basic_string<charT, ST, SA>& s, 
              const reg_expression<charT, traits, Allocator>& e, 
              unsigned flags = match_default);

¼çÍ×ÈÇ(ÌõÃí:´Ê°×ÈǤǤϤʤ¤ÄêµÁ)¤Î´Ø¿ô¤Î¥Ñ¥é¥á¡¼¥¿¤Ï¼¡¤ÎÄ̤ê¤Ç¤¢¤ë¡£
 

  foo ½Ò¸ì´Ø¿ô¥ª¥Ö¥¸¥§¥¯¥È¤ä´Ø¿ô¥Ý¥¤¥ó¥¿¡£ ¤è¤ê¾Ü¤·¤¤¾ðÊó¤Ï²¼¤ò¸«¤è¡£  
  first ¸¡º÷ÈϰϤÎÀèÆ¬¡£  
  last ¸¡º÷ÈϰϤÎËöü¡£  
  e ¸¡º÷¤Î¤¿¤á¤ÎÀµµ¬É½¸½¡£  
  flags ¤É¤Î¤è¤¦¤Ë°ìÃפ¬¹Ô¤ï¤ì¤ë¤«¤ò·èÄꤹ¤ë¥Õ¥é¥°¡£ match_flags Îóµó»Ò¤Î¤Ò¤È¤Ä¡£  

 ¤³¤Î¥¢¥ë¥´¥ê¥º¥à¤Ï¡¢Àµµ¬É½¸½ e ¤ËÂФ¹¤ë¡¢ ½Å¤Ê¤é¤Ê¤¤Á´¤Æ¤Î°ìÃפòȯ¸«¤¹¤ë¡£¤½¤ì¤¾¤ì¤Î°ìÃפǡ¢ match_results<iterator, Allocator> ¹½Â¤ÂΤ˾ðÊ󤬯þ¤ë¡£¤³¤ì¤Ï²¿¤¬°ìÃפ·¤¿¤«¤Ë¤Ä¤¤¤Æ¤Î¾ðÊó¤òÊÝ»ý¤·¡¢ ½Ò¸ì´Ø¿ô foo ¤ò¸Æ¤Ó½Ð¤·¡¢¤Ò¤È¤Ä¤Î°ú¿ô¤È¤·¤Æ match_results<iterator,Allocator> ¤òÅϤ¹¡£ ¤â¤·½Ò¸ì´Ø¿ô¤¬ true ¤òÊÖ¤»¤Ð grep Áàºî¤Ï°ú¤­Â³¤­¹Ô¤ï¤ì¤ë¡£ ¤½¤¦¤Ç¤Ê¤±¤ì¤Ð grep Áàºî¤Ï¤½¤ì°Ê¹ß¤Î°ìÃפò¸¡º÷¤¹¤ë¤³¤È¤Ê¤¯½ªÎ»¤¹¤ë¡£ ¤³¤Î´Ø¿ô¤Ï¡¢È¯¸«¤µ¤ì¤¿°ìÃפοô¤òÊÖ¤¹¡£

½Ò¸ì´Ø¿ô¤Î°ìÈÌŪ¤Ê·Á¼°¤Ï°Ê²¼¤ÎÄ̤ê¤Ç¤¢¤ë:

struct grep_predicate
{
   bool operator()(const match_results<iterator_type, expression_type::alloc_type>& m);
};

Î㤨¤Ð¡¢Àµµ¬É½¸½ "a*b" ¤Ïʸ»úÎó "aaaaab" ¤ÎÃæ¤Ç¤Ò¤È¤Ä¤Î°ìÃפòȯ¸«¤·¡¢Ê¸»úÎó "aaabb" ¤ÎÃæ¤ËÆó¤Ä¤Î°ìÃפòȯ¸«¤¹¤ë¡£

¤³¤Î¥¢¥ë¥´¥ê¥º¥à¤Ï¡¢grep ¤Î¼ÂÁõ¤è¤ê¤â¿¤¯¤Î¤³¤È¤Ë»È¤¦¤³¤È¤¬½ÐÍè¤ë¤³¤È¤ò³Ð¤¨¤Æ¤ª¤¯¤³¤È¡£ ½Ò¸ì´Ø¿ô¤¬¤¢¤ë¤Î¤Ç¡¢¤ä¤ê¤¿¤¤¤³¤È¤Ï¤½¤³¤Ç²¿¤Ç¤â½ÐÍè¤ë¡£ grep ¥æ¡¼¥Æ¥£¥ê¥Æ¥£¤Ï·ë²Ì¤ò²èÌ̤˽ÐÎϤ¹¤ë¡£ ÊÌ¤Î¥×¥í¥°¥é¥à¤ÏÀµµ¬É½¸½¤Ë´ð¤Å¤¤¤Æ¥Õ¥¡¥¤¥ë¤òº÷°úÉÕ¤±¤·¡¢¥Ö¥Ã¥¯¥Þ¡¼¥¯¤Î½¸¹ç¤ò ¥ê¥¹¥È¤ËÃߤ¨¤ë¤³¤È¤¬½ÐÍè¤ë¡£ ¥Æ¥­¥¹¥È¥Õ¥¡¥¤¥ëÊÑ´¹¥æ¡¼¥Æ¥£¥ê¥Æ¥£¤Ï¥Õ¥¡¥¤¥ë¤Ë½ÐÎϤ¹¤ë¡£ ºÆµ¢Åª¤Ê²òÀϤΤ¿¤á¤Ë¡¢ ¤Ò¤È¤Ä¤Î regex_grep ¤Î·ë²Ì¤òÊ̤Πregex_grep ¤ËÏ¢º¿¤¹¤ë¤³¤È¤µ¤¨²Äǽ¤Ç¤¢¤ë¡£

Example: regex_search ¤ÎÎã¤ò¡¢Âå¤ï¤ê¤Ë regex_grep ¤ò»È¤Ã¤ÆÊÑ´¹¤·¤Æ¤¤¤ë:

#include <string> 
#include <map> 
#include <boost/regex.hpp> 

// IndexClasses: 
// takes the contents of a file in the form of a string 
// and searches for all the C++ class definitions, storing 
// their locations in a map of strings/int's 

typedef std::map<std::string, int, std::less<std::string> > map_type; 

boost::regex expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" 
                 "(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?[[:space:]]*)*(\\<\\w*\\>)" 
                 "[[:space:]]*(<[^;:{]+>[[:space:]]*)?(\\{|:[^;\\{()]*\\{)"); 

class IndexClassesPred 
{ 
   map_type& m; 
   std::string::const_iterator base; 
public: 
   IndexClassesPred(map_type& a, std::string::const_iterator b) : m(a), base(b) {} 
   bool operator()(const match_results<std::string::const_iterator, regex::alloc_type>& what) 
   { 
      // what[0] contains the whole string 
      // what[5] contains the class name. 
      // what[6] contains the template specialisation if any. 
      // add class name and position to map: 
      m[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = 
                what[5].first - base; 
      return true; 
   } 
}; 

void IndexClasses(map_type& m, const std::string& file) 
{ 
   std::string::const_iterator start, end; 
   start = file.begin(); 
   end = file.end(); 
   regex_grep(IndexClassesPred(m, start), start, end, expression); 
} 

Example: ¥°¥í¡¼¥Ð¥ë¥³¡¼¥ë¥Ð¥Ã¥¯´Ø¿ô¸Æ¤Ó½Ð¤·¤Ë regex_grep ¤ò»È¤Ã¤Æ¤¤¤ë:

#include <string> 
#include <map> 
#include <boost/regex.hpp> 

// purpose: 
// takes the contents of a file in the form of a string 
// and searches for all the C++ class definitions, storing 
// their locations in a map of strings/int's 

typedef std::map<std::string, int, std::less<std::string> > map_type; 

boost::regex expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?(\\{|:[^;\\{()]*\\{)"); 

map_type class_index; 
std::string::const_iterator base; 

bool grep_callback(const boost::match_results<std::string::const_iterator, boost::regex::alloc_type>& what) 
{ 
   // what[0] contains the whole string 
   // what[5] contains the class name. 
   // what[6] contains the template specialisation if any. 
   // add class name and position to map: 
   class_index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = 
                what[5].first - base; 
   return true; 
} 

void IndexClasses(const std::string& file) 
{ 
   std::string::const_iterator start, end; 
   start = file.begin(); 
   end = file.end(); 
   base = start; 
   regex_grep(grep_callback, start, end, expression, match_default); 
}
  

Example: regex_grep ¤ò»È¤Ã¤Æ¥¯¥é¥¹¥á¥ó¥Ð´Ø¿ô¤ò¸Æ¤Ó½Ð¤·¤Æ¤¤¤ë¡£ ɸ½à¥é¥¤¥Ö¥é¥ê¥¢¥À¥×¥¿ std::mem_fun ¤È std::bind1st ¤Ï¥á¥ó¥Ð´Ø¿ô¤ò½Ò¸ì´Ø¿ô¤ËÊÑ´¹¤¹¤ë¤¿¤á¤Ë»È¤ï¤ì¤Æ¤¤¤ë¡£

#include <string> 
#include <map> 
#include <boost/regex.hpp> 
#include <functional> 

// purpose: 
// takes the contents of a file in the form of a string 
// and searches for all the C++ class definitions, storing 
// their locations in a map of strings/int's 

typedef std::map<std::string, int, std::less<std::string> > map_type; 

class class_index 
{ 
   boost::regex expression; 
   map_type index; 
   std::string::const_iterator base; 
   bool grep_callback(boost::match_results<std::string::const_iterator, boost::regex::alloc_type> what); 
public: 
   void IndexClasses(const std::string& file); 
   class_index() 
      : index(), 
        expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" 
                   "(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?" 
                   "[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?" 
                   "(\\{|:[^;\\{()]*\\{)" 
                   ){} 
}; 

bool class_index::grep_callback(boost::match_results<std::string::const_iterator, boost::regex::alloc_type> what) 
{ 
   // what[0] contains the whole string 
   // what[5] contains the class name. 
   // what[6] contains the template specialisation if any. 
   // add class name and position to map: 
   index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = 
               what[5].first - base; 
   return true; 
} 

void class_index::IndexClasses(const std::string& file) 
{ 
   std::string::const_iterator start, end; 
   start = file.begin(); 
   end = file.end(); 
   base = start; 
   regex_grep(std::bind1st(std::mem_fun(&class_index::grep_callback), this), 
              start, 
              end, 
              expression); 
} 
  

ºÇ¸å¤Ë¡¢ C++ Builder ¤Î¥æ¡¼¥¶¤Ï C++ Builder ¤Î¥¯¥í¡¼¥¸¥ã·¿¤ò¥³¡¼¥ë¥Ð¥Ã¥¯°ú¿ô¤È¤·¤Æ»È¤¦¤³¤È¤¬½ÐÍè¤ë¡£

#include <string> 
#include <map> 
#include <boost/regex.hpp> 
#include <functional> 

// purpose: 
// takes the contents of a file in the form of a string 
// and searches for all the C++ class definitions, storing 
// their locations in a map of strings/int's 

typedef std::map<std::string, int, std::less<std::string> > map_type; 
class class_index 
{ 
   boost::regex expression; 
   map_type index; 
   std::string::const_iterator base; 
   typedef boost::match_results<std::string::const_iterator, boost::regex::alloc_type> arg_type; 
   bool grep_callback(const arg_type& what); 
public: 
   typedef bool (__closure* grep_callback_type)(const arg_type&); 
   void IndexClasses(const std::string& file); 
   class_index() 
      : index(), 
        expression("^(template[[:space:]]*<[^;:{]+>[[:space:]]*)?" 
                   "(class|struct)[[:space:]]*(\\<\\w+\\>([[:blank:]]*\\([^)]*\\))?" 
                   "[[:space:]]*)*(\\<\\w*\\>)[[:space:]]*(<[^;:{]+>[[:space:]]*)?" 
                   "(\\{|:[^;\\{()]*\\{)" 
                   ){} 
}; 

bool class_index::grep_callback(const arg_type& what) 
{ 
   // what[0] contains the whole string    
// what[5] contains the class name.    
// what[6] contains the template specialisation if any.    
// add class name and position to map:    
index[std::string(what[5].first, what[5].second) + std::string(what[6].first, what[6].second)] = 
               what[5].first - base; 
   return true; 
} 

void class_index::IndexClasses(const std::string& file) 
{ 
   std::string::const_iterator start, end; 
   start = file.begin(); 
   end = file.end(); 
   base = start; 
   class_index::grep_callback_type cl = &(this->grep_callback); 
   regex_grep(cl, 
            start, 
            end, 
            expression); 
} 

 Algorithm regex_format

#include <boost/regex.hpp>

¥¢¥ë¥´¥ê¥º¥à regex_format ¤Ï°ìÃפηë²Ì¤ò¼õ¤±¼è¤ê¡¢ ½ñ¼°»ØÄê»Ò ¤Ë´ð¤Å¤¤¤Æ¿·¤·¤¤Ê¸»úÎó¤òºîÀ®¤¹¤ë¡£ regex_format ¤Ï¸¡º÷¤ÈÃÖ´¹¤ÎÁàºî¤Î°Ù¤Ë»È¤¦¤³¤È¤¬½ÐÍè¤ë:

template <class OutputIterator, class iterator, class Allocator, class charT>
OutputIterator regex_format(OutputIterator out,
                            const match_results<iterator, Allocator>& m,
                            const charT* fmt,
                            unsigned flags = 0);

template <class OutputIterator, class iterator, class Allocator, class charT>
OutputIterator regex_format(OutputIterator out,
                            const match_results<iterator, Allocator>& m,
                            const std::basic_string<charT>& fmt,
                            unsigned flags = 0);

¥é¥¤¥Ö¥é¥ê¤Ï¼¡¤Î¤è¤¦¤Ê regex_format ¤Î´Ê°×ÈǤâÍѰդ·¤Æ¤¤¤ë¡£ ¤³¤ì¤Ï·ë²Ì¤ò¡¢¥¤¥Æ¥ì¡¼¥¿¤Ë½ÐÎϤ¹¤ë¤Î¤Ç¤Ï¤Ê¤¯¡¢Ê¸»úÎó¤È¤·¤ÆÄ¾ÀÜÊÖ¤¹¡£ [Ãí°Õ: ¤³¤Î´Ê°×ÈǤϥ³¥ó¥Ñ¥¤¥é¤Ë¤è¤Ã¤Æ¤Ï»È¤¨¤Ê¤¤¤«¡¢»È¤¨¤Æ¤âµ¡Ç½¤¬¸Â¤é¤ì¤Æ¤¤¤ë¤«¤â¤·¤ì¤Ê¤¤¡£]

template <class iterator, class Allocator, class charT>
std::basic_string<charT> regex_format
                                 (const match_results<iterator, Allocator>& m, 
                                  const charT* fmt,
                                  unsigned flags = 0);

template <class iterator, class Allocator, class charT>
std::basic_string<charT> regex_format
                                 (const match_results<iterator, Allocator>& m, 
                                  const std::basic_string<charT>& fmt,
                                  unsigned flags = 0);

¼çÍ×ÈǤδؿô¤ËÅϤµ¤ì¤ë¥Ñ¥é¥á¡¼¥¿¤Ï¼¡¤ÎÄ̤ê¤Ç¤¢¤ë¡£
 

  OutputIterator out ½ÐÎÏ¥¤¥Æ¥ì¡¼¥¿·¿¡£ ½ÐÎÏʸ»úÎó¤Ï¤³¤Î¥¤¥Æ¥ì¡¼¥¿¤ËÁ÷¤é¤ì¤ë¡£ Ä̾ï¤Ï std::ostream_iterator ¤Ç¤¢¤ë¡£  
  const match_results<iterator, Allocator>& m match_result<> ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¡£ ¤³¤ì¤Ï¾å¤Î°ìÃ×ȽÄꥢ¥ë¥´¥ê¥º¥à¤Î¤¦¤Á¤Î¤Ò¤È¤Ä¤«¤éÆÀ¤é¤ì¡¢ ²¿¤¬°ìÃפ·¤¿¤òɽ¤¹¡£  
  const charT* fmt °ìÃפ¬¤É¤Î¤è¤¦¤Ë¿·¤·¤¤Ê¸»úÎó¤ËÊÑ´¹¤µ¤ì¤ë¤«¤ò ·èÄꤹ¤ë½ñ¼°»ØÄê»Ò¡£  
  unsigned flags ½ñ¼°»ØÄê»Ò¤¬¤É¤Î¤è¤¦¤Ë²ò¼á¤µ¤ì¤ë¤«¤ò¼¨¤¹¡¢ ¥ª¥×¥·¥ç¥ó¤Î¥Õ¥é¥°¡£  

½ñ¼°¤Î¥Õ¥é¥°¤Ï°Ê²¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë¡£

 

  format_all ¤¹¤Ù¤Æ¤Î¹½Ê¸¤Î¥ª¥×¥·¥ç¥ó¤ò²Äǽ¤Ë¤¹¤ë¡£ (³ÈÄ¥Àµµ¬É½¸½¹½Ê¸¤Ë²Ã¤¨ perl-like ¤Î¹½Ê¸¤â²Äǽ)¡£  
  format_sed sed-like ¤Î¹½Ê¸¤Î¤ßµö¤¹¡£  
  format_perl perl-like ¤Î¹½Ê¸¤Î¤ßµö¤¹¡£  
  format_no_copy regex_merge Áàºî¤Î´Ö¤Ë¡¢°ìÃפ·¤Ê¤«¤Ã¤¿Éôʬ¤ò½ÐÎÏʸ»úÎó¤Ë¥³¥Ô¡¼¤Ç¤­¤Ê¤¯¤¹¤ë¡£  
  format_first_only ¤³¤Î¥Õ¥é¥°¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ë¤È¤­¤Ï¡¢ºÇ½é¤Î°ìÃפÀ¤±¤¬ÃÖ´¹¤µ¤ì¤ë¡£ (regex_merge ¤À¤±¤ËŬÍѤµ¤ì¤ë¡£)  


 

½ñ¼°»ØÄê»Ò¤Î¹½Ê¸(¤½¤·¤ÆÍøÍѲÄǽ¤Ê¥ª¥×¥·¥ç¥ó)¤Ï ½ñ¼°»ØÄê»Ò ¤Ç¤è¤ê´°Á´¤Ë½ñ¤«¤ì¤Æ¤¤¤ë¡£


Algorithm regex_merge

#include <boost/regex.hpp>

¥¢¥ë¥´¥ê¥º¥à regex_merge ¤Ï regex_grep ¤È regex_format ¤òÁȤ߹ç¤ï¤»¤¿¤â¤Î¤Ç¤¢¤ë¡£ ¤Ä¤Þ¤ê¡¢Ê¸»úÎó¤ÎºÇ½é¤«¤éºÇ¸å¤Þ¤Ç¡¢Àµµ¬É½¸½¤È¤ÎÁ´¤Æ¤Î°ìÃפòȯ¸«¤·¡¢ ¤½¤Î°ìÃפ½¤ì¤¾¤ì¤ËÂФ·¤Æ¡¢Ê¸»úÎó¤ò½ñ¼°²½¤¹¤ë¤¿¤á¤Ë regex_format ¤ò¸Æ¤Ó½Ð¤·¡¢ ·ë²Ì¤ò½ÐÎÏ¥¤¥Æ¥ì¡¼¥¿¤ËÁ÷¤ë¡£°ìÃפ·¤Ê¤«¤Ã¤¿Éôʬ¤Î¥Æ¥­¥¹¥È¤Ï ¥Õ¥é¥°¥Ñ¥é¥á¡¼¥¿ format_no_copy ¤¬ÀßÄꤵ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢Êѹ¹¤µ¤ì¤ë¤³¤È¤Ê¤¯½ÐÎϤµ¤ì¤ë¡£ format_first_only ¤¬ÀßÄꤵ¤ì¤Æ¤¤¤ì¤Ð¡¢Á´¤Æ¤Î°ìÃפǤϤʤ¯¡¢ºÇ½é¤Î°ìÃפÀ¤±¤¬ÃÖ´¹¤µ¤ì¤ë¡£

template <class OutputIterator, class iterator, class traits, class Allocator, class charT>
OutputIterator regex_merge(OutputIterator out, 
                          iterator first,
                          iterator last,
                          const reg_expression<charT, traits, Allocator>& e, 
                          const charT* fmt, 
                          unsigned int flags = match_default);

template <class OutputIterator, class iterator, class traits, class Allocator, class charT>
OutputIterator regex_merge(OutputIterator out, 
                           iterator first,
                           iterator last,
                           const reg_expression<charT, traits, Allocator>& e, 
                           std::basic_string<charT>& fmt, 
                           unsigned int flags = match_default);

¤³¤Î¥é¥¤¥Ö¥é¥ê¤Ï¼¡¤Î¤è¤¦¤Ê regex_merge ¤Î´Ê°×ÈǤâÄêµÁ¤·¤Æ¤¤¤ë¡£ ¤³¤ì¤Ï¡¢¥¤¥Æ¥ì¡¼¥¿¤Ë½ÐÎϤ¹¤ë¤Î¤Ç¤Ï¤Ê¤¯¡¢·ë²Ì¤òʸ»úÎó¤È¤·¤ÆÄ¾ÀÜÊÖ¤¹¡£ [Ãí°Õ¡§¥³¥ó¥Ñ¥¤¥é¤Ë¤è¤Ã¤Æ¤Ï¤³¤Î´Ê°×ÈǤÏÍøÍѤǤ­¤Ê¤¤¤«¡¢ÍøÍѤǤ­¤Æ¤â ¸Â¤é¤ì¤¿·Á¤Ç¤·¤«»È¤¨¤Ê¤¤¡£]

template <class traits, class Allocator, class charT>
std::basic_string<charT> regex_merge(const std::basic_string<charT>& text,
                                     const reg_expression<charT, traits, Allocator>& e, 
                                     const charT* fmt, 
                                     unsigned int flags = match_default);

template <class traits, class Allocator, class charT>
std::basic_string<charT> regex_merge(const std::basic_string<charT>& text,
                                     const reg_expression<charT, traits, Allocator>& e, 
                                     const std::basic_string<charT>& fmt, 
                                     unsigned int flags = match_default);

¼çÍ×ÈǤδؿô¤ËÅϤµ¤ì¤ë¥Ñ¥é¥á¡¼¥¿¤Ï¼¡¤ÎÄ̤ê¤Ç¤¢¤ë:
 

  OutputIterator out ½ÐÎÏ¥¤¥Æ¥ì¡¼¥¿·¿¡£ Ä̾ï¤Ï std::ostream_iterator ¤Ç¤¢¤ë¡£  
  iterator first ¸¡º÷¤¹¤ë¥Æ¥­¥¹¥È¤ÎÈϰϤκǽ顣 (ÁÐÊý¸þ¥¤¥Æ¥ì¡¼¥¿)  
  iterator last ¸¡º÷¤¹¤ë¥Æ¥­¥¹¥È¤ÎÈϰϤκǸ塣 (ÁÐÊý¸þ¥¤¥Æ¥ì¡¼¥¿)  
  const reg_expression<charT, traits, Allocator>& e ¸¡º÷¤Î¤¿¤á¤ÎÀµµ¬É½¸½  
  const charT* fmt °ìÃפ·¤¿Éôʬ¤Î¥Æ¥­¥¹¥È¤ËŬÍѤµ¤ì¤ë½ñ¼°»ØÄê»Ò¡£  
  unsigned int flags = match_default Àµµ¬É½¸½¤¬¤É¤Î¤è¤¦¤Ë°ìÃפ¹¤ë¤«¤ò·èÄꤹ¤ë¥Õ¥é¥°¡£ match_flags ¤ò¸«¤è¡£ ¤Þ¤¿¡¢½ñ¼°»ØÄê»Ò¤¬¤É¤Î¤è¤¦¤Ë²ò¼á¤µ¤ì¤ë¤«¤ò·èÄꤹ¤ë¥Õ¥é¥°¡£ format_flags ¤ò¸«¤è¡£  

¼¡ Îã ¤Ï C/C++ ¤Î¥½¡¼¥¹¥³¡¼¥É¤ò¥¤¥ó¥×¥Ã¥È¤È¤·¤Æ¼õ¤±¼è¤ê¡¢ ¹½Ê¸¤¬¶¯Ä´¤µ¤ì¤¿ HTML ¥³¡¼¥É¤ò½ÐÎϤ¹¤ë¡£

#include <fstream>
#include <sstream>
#include <string>
#include <iterator>
#include <boost/regex.hpp>
#include <fstream>
#include <iostream>

// purpose:
// takes the contents of a file and transform to
// syntax highlighted code in html format

boost::regex e1, e2;
extern const char* expression_text;
extern const char* format_string;
extern const char* pre_expression;
extern const char* pre_format;
extern const char* header_text;
extern const char* footer_text;

void load_file(std::string& s, std::istream& is)
{
   s.erase();
   s.reserve(is.rdbuf()->in_avail());
   char c;
   while(is.get(c))
   {
      if(s.capacity() == s.size())
         s.reserve(s.capacity() * 3);
      s.append(1, c);
   }
}

int main(int argc, const char** argv)
{
   try{
   e1.assign(expression_text);
   e2.assign(pre_expression);
   for(int i = 1; i < argc; ++i)
   {
      std::cout << "Processing file " << argv[i] << std::endl;
      std::ifstream fs(argv[i]);
      std::string in;
      load_file(in, fs);
      std::string out_name(std::string(argv[i]) + std::string(".htm"));
      std::ofstream os(out_name.c_str());
      os << header_text;
      // strip '<' and '>' first by outputting to a
      // temporary string stream
      std::ostringstream t(std::ios::out | std::ios::binary);
      std::ostream_iterator<char, char> oi(t);
      boost::regex_merge(oi, in.begin(), in.end(), e2, pre_format);
      // then output to final output stream
      // adding syntax highlighting:
      std::string s(t.str());
      std::ostream_iterator<char, char> out(os);
      boost::regex_merge(out, s.begin(), s.end(), e1, format_string);
      os << footer_text;
   }
   }
   catch(...)
   { return -1; }
   return 0;
}

extern const char* pre_expression = "(<)|(>)|\\r";
extern const char* pre_format = "(?1<)(?2>)";


const char* expression_text = // preprocessor directives: index 1
                              "(^[[:blank:]]*#(?:[^\\\\\\n]|\\\\[^\\n[:punct:][:word:]]*[\\n[:punct:][:word:]])*)|"
                              // comment: index 2
                              "(//[^\\n]*|/\\*.*?\\*/)|"
                              // literals: index 3
                              "\\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\>|"
                              // string literals: index 4
                              "('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|"
                              // keywords: index 5
                              "\\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import"
                              "|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall"
                              "|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool"
                              "|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete"
                              "|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto"
                              "|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected"
                              "|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast"
                              "|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned"
                              "|using|virtual|void|volatile|wchar_t|while)\\>"
                              ;

const char* format_string = "(?1<font color=\"#008040\">$&</font>)"
                            "(?2<I><font color=\"#000080\">$&</font></I>)"
                            "(?3<font color=\"#0000A0\">$&</font>)"
                            "(?4<font color=\"#0000FF\">$&</font>)"
                            "(?5<B>$&</B>)";

const char* header_text = "<HTML>\n<HEAD>\n"
                          "<TITLE>Auto-generated html formated source</TITLE>\n"
                          "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=windows-1252\">\n"
                          "</HEAD>\n"
                          "<BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\">\n"
                          "<P> </P>\n<PRE>";

const char* footer_text = "</PRE>\n</BODY>\n\n";

Algorithm regex_split

#include <boost/regex.hpp>

¥¢¥ë¥´¥ê¥º¥à regex_split ¤Ï perl ¤Î spilit Áàºî¤È»÷¤¿¤è¤¦¤Ê¤³¤È¤ò¤ª¤³¤Ê¤¦¡£ ¤½¤·¤Æ3¼ïÎà¤Î¥ª¡¼¥Ð¥í¡¼¥É¤Î·Á¤ÇÄ󶡤µ¤ì¤Æ¤¤¤ë¡£

template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2, class Alloc2>
std::size_t regex_split(OutputIterator out, 
                        std::basic_string<charT, Traits1, Alloc1>& s, 
                        const reg_expression<charT, Traits2, Alloc2>& e,
                        unsigned flags,
                        std::size_t max_split);

template <class OutputIterator, class charT, class Traits1, class Alloc1, class Traits2, class Alloc2>
std::size_t regex_split(OutputIterator out, 
                        std::basic_string<charT, Traits1, Alloc1>& s, 
                        const reg_expression<charT, Traits2, Alloc2>& e,
                        unsigned flags = match_default);

template <class OutputIterator, class charT, class Traits1, class Alloc1>
std::size_t regex_split(OutputIterator out, 
                        std::basic_string<charT, Traits1, Alloc1>& s);

¤½¤ì¤¾¤ì¤Î´Ø¿ô¤Ï½ÐÎϤΤ¿¤á¤Î½ÐÎÏ¥¤¥Æ¥ì¡¼¥¿¡¢ÆþÎÏʸ»úÎó¤ò¼õ¤±¼è¤ë¡£ Àµµ¬É½¸½¤¬¡¢°õÉÕ¤±¤µ¤ì¤¿»Òɽ¸½¤ò´Þ¤Þ¤Ê¤¤¤È¤­¡¢ ¥¢¥ë¥´¥ê¥º¥à¤ÏÀµµ¬É½¸½¤È°ìÃפ·¤Ê¤¤¥Æ¥­¥¹¥È¤Î¤½¤ì¤¾¤ì¤ÎÉôʬ¤ò ¤Ò¤È¤Ä¤Îʸ»úÎó¤È¤·¤Æ¡¢½ÐÎÏ¥¤¥Æ¥ì¡¼¥¿¤Ë½ñ¤­¹þ¤à¡£ ¤â¤·Àµµ¬É½¸½¤¬°õÉÕ¤±¤µ¤ì¤¿»Òɽ¸½¤ò´Þ¤ó¤Ç¤¤¤ì¤Ð¡¢°ìÃפ¬È¯¸«¤µ¤ì¤ëÅ٤ˡ¢ °õÉÕ¤±¤µ¤ì¤¿»Òɽ¸½¤½¤ì¤¾¤ì¤ËÂФ·¤Æ¤Ò¤È¤Ä¤Îʸ»úÎ󤬡¢ ½ÐÎÏ¥¤¥Æ¥ì¡¼¥¿¤Ë½ñ¤­¹þ¤Þ¤ì¤ë¡£ max_split °Ê¾å¤Îʸ»úÎó¤Ï½ÐÎÏ¥¤¥Æ¥ì¡¼¥¿¤Ë½ñ¤­¹þ¤Þ¤ì¤Ê¤¤¡£ ´Ø¿ô¤òÈ´¤±¤ëÁ°¤Ë¡¢½èÍý¤µ¤ì¤¿Á´¤Æ¤ÎÆþÎϤÏʸ»úÎó s ¤«¤éºï½ü¤µ¤ì¤ë (¤â¤· max_split ¤Þ¤ÇÅþ㤷¤Ê¤±¤ì¤Ð¡¢Á´¤Æ¤Î s ¤¬ºï½ü¤µ¤ì¤ë)¡£ ½ÐÎÏ¥¤¥Æ¥ì¡¼¥¿¤Ë½ñ¤­¹þ¤Þ¤ì¤¿Ê¸»úÎó¤Î¿ô¤òÊÖ¤¹¡£ ¤â¤·¥Ñ¥é¥á¡¼¥¿ max_split ¤¬ÆÃÄꤵ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢ ¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï UINT_MAX ¤¬»È¤ï¤ì¤ë¡£ Àµµ¬É½¸½¤¬ÆÃÄꤵ¤ì¤Æ¤¤¤Ê¤±¤ì¤Ð¡¢¥Ç¥Õ¥©¥ë¥È¤Ç¤Ï "\s+" ¤¬»È¤ï¤ì¡¢¶õÇò¤Ë¤è¤Ã¤ÆÊ¬³ä¤µ¤ì¤ë¡£ (ÌõÃí: ¤³¤Î´Ø¿ô¤Ï¡¢Àµµ¬É½¸½¤¬»Òɽ¸½¤ò»ý¤Ã¤Æ¤¤¤ë¤«¤É¤¦¤«¤Ç¡¢Æ°ºî¤¬Â礭¤¯°Û¤Ê¤ë¡£ Àµµ¬É½¸½¤¬»Òɽ¸½¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¤È¤­¡¢¤³¤Î´Ø¿ô¤Ï¤Þ¤µ¤Ë¡¢ perl ¤Î split ¤ÈƱÍÍ¤ËÆ°ºî¤¹¤ë¡£ Àµµ¬É½¸½¤¬»Òɽ¸½¤ò»ý¤Ã¤Æ¤¤¤ë¤È¤­¡¢¤½¤Î°ìÃפ·¤¿»Òɽ¸½¤¬½ÐÎÏ¥¤¥Æ¥ì¡¼¥¿¤Ë½ñ¤­¹þ¤Þ¤ì¤ë¡£)

Example: the following function will split the input string into a series of tokens, and remove each token from the string s:

unsigned tokenise(std::list<std::string>& l, std::string& s)
{
   return boost::regex_split(std::back_inserter(l), s);
}

Example: the following short program will extract all of the URL's from a html file, and print them out to cout:

#include <list>
#include <fstream>
#include <iostream>
#include <boost/regex.hpp>

boost::regex e("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"",
               boost::regbase::normal | boost::regbase::icase);

void load_file(std::string& s, std::istream& is)
{
   s.erase();
   //
   // attempt to grow string buffer to match file size,
   // this doesn't always work...
   s.reserve(is.rdbuf()-&gtin_avail());
   char c;
   while(is.get(c))
   {
      // use logarithmic growth stategy, in case
      // in_avail (above) returned zero:
      if(s.capacity() == s.size())
         s.reserve(s.capacity() * 3);
      s.append(1, c);
   }
}


int main(int argc, char** argv)
{
   std::string s;
   std::list<std::string> l;

   for(int i = 1; i < argc; ++i)
   {
      std::cout << "Findings URL's in " << argv[i] << ":" << std::endl;
      s.erase();
      std::ifstream is(argv[i]);
      load_file(s, is);
      boost::regex_split(std::back_inserter(l), s, e);
      while(l.size())
      {
         s = *(l.begin());
         l.pop_front();
         std::cout << s << std::endl;
      }
   }
   return 0;
}

Partial Matches

°ìÃץե饰 match_partial ¤Ï¼¡¤Î¥¢¥ë¥´¥ê¥º¥à¤ËÅϤ¹¤³¤È¤¬½ÐÍè¤ë: regex_match, regex_search, ¤½¤·¤Æ regex_grep ¡£ ¤³¤ì¤Ï¡¢Á´ÂΰìÃ×¤ÈÆ±ÍͤËÉôʬ°ìÃפ¬È¯¸«¤µ¤ì¤ë¤³¤È¤òɽ¤¹¡£ Éôʬ°ìÃפϡ¢ÆþÎϥƥ­¥¹¥È¤Î½ªÃ¼¤Ë1ʸ»ú°Ê¾å¤Î°ìÃפ¬¤¢¤ë¤¬¡¢ ¤½¤ì¤¬Àµµ¬É½¸½¤ÎÁ´ÂΤȰìÃפ·¤Æ¤¤¤ë¤Î¤Ç¤Ï¤Ê¤¤¡¢¤È¤¤¤¦¤â¤Î¤Ç¤¢¤ë (¥Æ¥­¥¹¥È¤¬½ªÎ»¤·¤¿¤«¤â¤·¤ì¤Ê¤¤¤¬¡¢¤Þ¤À³¤¯¤«¤â¤·¤ì¤Ê¤¤¡¢¤È¤¤¤¦¤è¤¦¤Ê»þ¤À)¡£ Éôʬ°ìÃפÏÄ̾ÆþÎϥǡ¼¥¿¤Î¸¡¾Ú(¥­¡¼¥Ü¡¼¥É¤¬²¡¤µ¤ì¤ëÅÙ¤Ë ¤½¤ì¤¾¤ì¤Îʸ»ú¤ò¸¡¾Ú¤¹¤ë)¡¢¤È¤«¡¢¥á¥â¥ê(¤ä¥á¥â¥ê¥Þ¥Ã¥×¥É¥Õ¥¡¥¤¥ë¤Ë¤µ¤¨) ¤ËÆÉ¤ß¹þ¤à¤Ë¤ÏŤ¹¤®¤ë¤è¤¦¤Ê¥Æ¥­¥¹¥È¡¢¤Þ¤¿¤ÏÉÔ³ÎÄê¤ÊŤµ¤Î¥Æ¥­¥¹¥È (Î㤨¤Ð¥Æ¥­¥¹¥È¤Î¸µ¤Ï¥½¥±¥Ã¥È¤ä¡¢¤½¤ì¤Ë»÷¤¿¤è¤¦¤Ê¤â¤Î¤«¤â¤·¤ì¤Ê¤¤) ¤ò¸¡º÷¤¹¤ë¤È¤­¤Ç¤¢¤ë¡£ Éôʬ°ìÃפÈÁ´ÂΰìÃפϼ¡¤Îɽ¤Ë¼¨¤µ¤ì¤ë¤è¤¦¤Ë¡¢¶èÊ̤µ¤ì¤Æ¤¤¤ë (ÊÑ¿ô M ¤Ï regex_match, regex_search, regex_grep ¤Ë¤è¤Ã¤Æ·ë²Ì¤ò ½ñ¤­¹þ¤Þ¤ì¤¿ match_results<> ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤òɽ¤·¤Æ¤¤¤ë)¡£

  Result M[0].matched M[0].first M[0].second
°ìÃ×̵¤· False ̤ÄêµÁ ̤ÄêµÁ ̤ÄêµÁ
Éôʬ°ìÃ× True False Éôʬ°ìÃפÎÀèÆ¬ Éôʬ°ìÃפνªÃ¼(¥Æ¥­¥¹¥È¤Î½ªÃ¼)
Á´ÂΰìÃ× True True Á´ÂΰìÃפÎÀèÆ¬ Á´ÂΰìÃפνªÃ¼

¼¡¤ÎÎ㠤ϥ桼¥¶¤¬¥­¡¼¤ò²¡¤¹Å٤ˡ¢¥Æ¥­¥¹¥È¤¬Í­¸ú¤Ê¥¯¥ì¥¸¥Ã¥È¥«¡¼¥É¤ÎÈֹ椫¤É¤¦¤«¸¡¾Ú¤¹¤ë¡£ ÆþÎϤµ¤ì¤¿Ê¸»ú¤ÏÀѤ߾夲¤é¤ì¤¿Ê¸»úÎó¤ËÉÕ¤±²Ã¤¨¤é¤ì¡¢ is_possible_card_number ¤ËÅϤµ¤ì¤ë¡£ ¤â¤·¤³¤ì¤¬ true ¤òÊÖ¤»¤Ð¥Æ¥­¥¹¥È¤ÏÍ­¸ú¤Ê¥«¡¼¥ÉÈÖ¹æ¤Ç¤¢¤ê¡¢ ¥æ¡¼¥¶¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Î OK ¥Ü¥¿¥ó¤¬Í­¸ú¤Ë¤Ê¤ë¡£ ¤â¤· false ¤òÊÖ¤»¤Ð¤³¤ì¤Ï¤Þ¤ÀÍ­¸ú¤Ê¥«¡¼¥É¥Ê¥ó¥Ð¡¼¤Ç¤Ï¤Ê¤¤¤¬¡¢ ¹¹¤ËÆþÎϤ¬²Äǽ¤Ç¡¢¥æ¡¼¥¶¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Î OK ¥Ü¥¿¥ó¤Ï̵¸ú¤Ç¤¢¤ë¡£ ºÇ¸å¤Ë¤³¤Î¼ê³¤­¤Ç¡¢ÆþÎϤ¬·è¤·¤ÆÍ­¸ú¤ÊÈÖ¹æ¤Ë¤Ï¤Ê¤é¤Ê¤¤¤È¤¤¤¦Îã³°¤¬ ȯÀ¸¤¹¤ì¤Ð¡¢ÆþÎϤµ¤ì¤¿Ê¸»ú¤ÏÇË´þ¤µ¤ì¡¢¥æ¡¼¥¶¤ËŬ¤·¤¿¥¨¥é¡¼¤òɽ¼¨¤¹¤ë¡£

#include <string>
#include <iostream>
#include <boost/regex.hpp>

boost::regex e("(\\d{3,4})[- ]?(\\d{4})[- ]?(\\d{4})[- ]?(\\d{4})");

bool is_possible_card_number(const std::string& input)
{
   //
   // return false for partial match, true for full match, or throw for
   // impossible match based on what we have so far...
   boost::match_results<std::string::const_iterator> what;
   if(0 == boost::regex_match(input, what, e, boost::match_default | boost::match_partial))
   {
      // the input so far could not possibly be valid so reject it:
      throw std::runtime_error("Invalid data entered - this could not possibly be a valid card number");
   }
   // OK so far so good, but have we finished?
   if(what[0].matched)
   {
      // excellent, we have a result:
      return true;
   }
   // what we have so far is only a partial match...
   return false;
}

¼¡¤ÎÎ㠤Ǥϡ¢ ÉÔÄê¤ÊŤµ¤Î¥Æ¥­¥¹¥È¤ò´Þ¤à¥¹¥È¥ê¡¼¥à¤«¤éÆþÎϤò¼õ¤±¼è¤ë¡£ ¤³¤ÎÎã¤Ïñ¤Ë¡¢¥¹¥È¥ê¡¼¥à¤ÎÃæ¤Ë¸½¤ì¤ë html ¥¿¥°¤Î¿ô¤ò¿ô¤¨¤ë¤À¤±¤Ç¤¢¤ë¡£ ¥Æ¥­¥¹¥È¤Ï¥Ð¥Ã¥Õ¥¡¤ËÆÉ¤ß¹þ¤Þ¤ì¡¢Æ±»þ¤Ë°ìÉô¤ò¸¡º÷¤¹¤ë¡£ Éôʬ°ìÃפ¬¸½¤ì¤ì¤Ð¡¢¤½¤ÎÉôʬ°ìÃפϡ¢¼¡¤Î¤Ò¤È¤Þ¤È¤Þ¤ê¤Î¥Æ¥­¥¹¥È¤ÎÀèÆ¬¤È¤·¤Æ ¼¡²ó¤Ë¸¡º÷¤µ¤ì¤ë:

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <boost/regex.hpp>

// match some kind of html tag:
boost::regex e("<[^>]*>");
// count how many:
unsigned int tags = 0;
// saved position of partial match:
char* next_pos = 0;

bool grep_callback(const boost::match_results<char*>& m)
{
   if(m[0].matched == false)
   {
      // save position and return:
      next_pos = m[0].first;
   }
   else
      ++tags;
   return true;
}

void search(std::istream& is)
{
   char buf[4096];
   next_pos = buf + sizeof(buf);
   bool have_more = true;
   while(have_more)
   {
      // how much do we copy forward from last try:
      unsigned leftover = (buf + sizeof(buf)) - next_pos;
      // and how much is left to fill:
      unsigned size = next_pos - buf;
      // copy forward whatever we have left:
      memcpy(buf, next_pos, leftover);
      // fill the rest from the stream:
      unsigned read = is.readsome(buf + leftover, size);
      // check to see if we've run out of text:
      have_more = read == size;
      // reset next_pos:
      next_pos = buf + sizeof(buf);
      // and then grep:
      boost::regex_grep(grep_callback,
                        buf,
                        buf + read + leftover,
                        e,
                        boost::match_default | boost::match_partial);
   }
}

Copyright Dr John Maddock 1998-2001 all rights reserved.


Japanese Translation Copyright (C) 2003 Kohske Takahashi

¥ª¥ê¥¸¥Ê¥ë¤Î¡¢µÚ¤Ó¤³¤ÎÃøºî¸¢É½¼¨¤¬Á´¤Æ¤ÎÊ£À½¤ÎÃæ¤Ë¸½¤ì¤ë¸Â¤ê¡¢¤³¤Îʸ½ñ¤Î Ê£À½¡¢ÍøÍÑ¡¢Êѹ¹¡¢ÈÎÇ䤽¤·¤ÆÇÛÉÛ¤òǧ¤á¤ë¡£¤³¤Î¥É¥­¥å¥á¥ó¥È¤Ï¡Ö¤¢¤ë¤¬¤Þ¤Þ¡× ¤ËÄ󶡤µ¤ì¤Æ¤ª¤ê¡¢¤¤¤«¤Ê¤ëÌÀ¼¨Åª¡¢°ÅÌÛŪÊݾڤâ¹Ô¤ï¤Ê¤¤¡£¤Þ¤¿¡¢ ¤¤¤«¤Ê¤ëÌÜŪ¤ËÂФ·¤Æ¤â¡¢¤½¤ÎÍøÍѤ¬Å¬¤·¤Æ¤¤¤ë¤³¤È¤ò´ØÃΤ·¤Ê¤¤¡£