显示标签为“regex”的博文。显示所有博文
显示标签为“regex”的博文。显示所有博文

2008年2月27日星期三

Boost regex Regular Express

[[:digit:]] 数字
[[:word:]] 字符加底划线

[:a-zA-Z:] 自定义字符集

\\\\ 一条\

Symbol Meaning
c Match the literal character c once, unless it is one of the special characters.
^ Match the beginning of a line.
. Match any character that isn't a newline.
$ Match the end of a line.
| Logical OR between expressions.
() Group subexpressions.
[] Define a character class.
* Match the preceding expression zero or more times.
+ Match the preceding expression one ore more times.
? Match the preceding expression zero or one time.
{n} Match the preceding expression n times.
{n,} Match the preceding expression at least n times.
{n, m} Match the preceding expression at least n times and at most m times.
d Match a digit.
D Match a character that is not a digit.
w Match an alpha character, including the underscore.
W Match a character that is not an alpha character.
s Match a whitespace character (any of t, n, r, or f).
S Match a non-whitespace character.
t Tab.
n Newline.
r Carriage return.
f Form feed.
m Escape m, where m is one of the metacharacters described above: ^, ., $, |, (), [], *, +, ?, , or /.

重要参考:
http://blog.chinaunix.net/u2/62093/showart_484483.html

2008年2月26日星期二

Boost regex backslash 问题

regex eps_change_blash("\"); //throw exception causes of a "bad_pattern".The problem is that
//have to escape the backslash once because of how C literals are interpreted
//and again because of how characters in regular expressions are interpreted.

Lösung:
regex eps_change_blash("\\");
or:
boost::regex expression("\", boost::regbase::literal);






Beispiel: use sourceDB(c:\backup\dbinfo.mdb) to repalce c:/apo_work/dbinfo.mdb
comes the problem "c:backupdbinfo.mdb"
Lösung: \\\\ for just once \

CString CDBinfoListCt::replaceSourceDB(CString s)
{
regex eps_sql_sourceDB("^.*\'c:\/apo_work\/dbinfo\.mdb\'$");
regex eps_sql_change_sourceDB("(c:/apo_work/dbinfo\.mdb)");
regex eps_change_blash("\\\\"); // \\\\ backslash for c, for boost... god!!!

cmatch what; // a array to store the matching string

std::string stdReplace;
CString sTmp;
CString sTmpDB;


if(regex_match(s, what, eps_sql_sourceDB))
{


stdReplace = (LPCTSTR)sourceDB;
stdReplace = boost::regex_replace(stdReplace,eps_change_blash,"\\\\\\\\");
sTmpDB.Format("%s",stdReplace.c_str());

sTmp.Format("%s",what[0]);
stdReplace = (LPCTSTR)sTmp;
stdReplace = boost::regex_replace(stdReplace,eps_sql_change_sourceDB,(LPCTSTR)sTmpDB);

sTmp.Format("%s",stdReplace.c_str());
return sTmp;

}
else
{
return s;

}


}

2008年1月28日星期一

VC++ boost: regex应用

下载boost_1_34_1.zip,解压
安装:
  1、打开vs2005在菜单tools中选择Visual Studio 2005 Command Prompt,打开已配置好环境的命令行。
  2、进入目录boost_1_34_1\libs\regex\build,
  编译文件:nmake -f vc8.mak
  安装(将编译好的文件复制到vs2005的特定目录下):nmake -f vc8.mak install
  删除临时文件:nmake -f vc8.mak clean
  3、Tools->Options->Projects and Solutions->VC++ Directories->Include files添加boost_1_34_1路径

  初次使用提示找不到libboost_regex-vc80-mt-gd-1_34_1.lib文件,到网上搜了下解决方法为:将 libboost_regex-vc80-mt-gd-1_34.lib改名libboost_regex-vc80-mt-gd-1_34_1.lib 放到vs或工程目录下。
4.
#include <boost/regex.hpp>
using namespace boost;
...
regex expression("^([0-9]{8})_([0-9]{6})_([[:word:]]+)\.([[:word:]]+)\.gz$");
cmatch what; // a array to store the matching string
if(regex_match(sName, what, expression
{
...