POCO中的字符串、文本和格式化(二)


从字符串中提取数值
Poco::NumberParser类的静态成员函数可以被用来从字符串中解析数值
#include "Poco/NumberParser.h"
int parse(const std::string& str)
从字符串中解析整数值。如果字符串中没有合法数字就抛出SyntaxException。
bool tryParse(const std::string& str, int& value)
从字符串中解析整数值并存储到value中。如果成功返回true,否则返回false,value值不可用。
unsigned parseUnsigned(const std::string& str)
bool tryParseUnsigned(const std::string& str, unsigned& value)
unsigned parseHex(const std::string& str)
bool tryParseHex(const std::string& str, unsigned& value)
Int64 parse64(const std::string& str)
bool tryParse64(const std::string& str Int64& value)
UInt64 parseUnsigned64(const std::string& str)
bool tryParseUnsigned64(const std::string& str UInt64& value)
UInt64 parseHex64(const std::string& str)
bool tryParseHex64(const std::string& str UInt64& value)
double parseFloat(const std::string& str)
bool tryParseFloat(const std::string& str, double& value)


标记化字符串
Poco::StringTokenizer用来把字符串分割为标记。
#include "Poco/StringTokenizer.h"
Poco::StringTokenizer创建时必须把字符串、分隔符、操作标示作为传递参数。
Poco::StringTokenizer内部容纳了一个包含提取标记的向量vector。
操作标示有:
TOK_IGNORE_EMPTY  空标记被忽略
TOK_TRIM  删除空格
#include "Poco/StringTokenizer.h"
#include "Poco/String.h" // for cat
using Poco::StringTokenizer;
using Poco::cat;
int main(int argc, char** argv)
{
StringTokenizer t1("red, green, blue", ",");
// "red", " green", " blue" (note the whitespace)
StringTokenizer t2("red,green,,blue", ",");
// "red", "green", "", "blue"
StringTokenizer t3("red; green, blue", ",;",StringTokenizer::TOK_TRIM);
// "red", "green", "blue"
StringTokenizer t4("red; green,, blue", ",;",StringTokenizer::TOK_TRIM |StringTokenizer::TOK_IGNORE_EMPTY);
// "red", "green", "blue"
std::string s(cat(std::string("; "), t4.begin(), t4.end()));
// "red; green; blue"
return 0;
}



正则表达式


通过Poco::RegularExpression类提供了支持正则表达式的能力。
#include "Poco/RegularExpression.h"
内部实现中,Poco::RegularExpression使用了PCRE库。
这意味着POCO中的正则表达式与Perl中的高度兼容。


Poco::RegularExpression支持:
1、匹配符合正则的字符串。
2、利用正则表达式提取子串。
3、利用正则替换子串。
不同的操作标示控制匹配行为。详见手册。
一些有用的操作标示:
RE_CASELESS  执行大小写敏感的匹配
RE_ANCHORED  把匹配模式视同以^开始
RE_NOTEMPTY  不匹配空字符串
RE_UTF8  匹配模式和主题是UTF-8编码
#include "Poco/RegularExpression.h"
#include <vector>
using Poco::RegularExpression;
int main(int argc, char** argv)
{
RegularExpression re1("[0-9]+");
bool match = re1.match("123"); // true
match = re1.match("abc");
// false
match = re1.match("abc123", 3); // true
RegularExpression::Match pos;
re1.match("123", 0, pos);
// pos.offset == 0, pos.length == 3
re1.match("ab12de", 0, pos); // pos.offset == 2, pos.length == 2
re1.match("abcd", 0, pos);
// pos.offset == std::string::npos
RegularExpression re2("([0-9]+) ([0-9]+)");
RegularExpression::MatchVec posVec;
re2.match("123 456", 0, posVec);
// posVec[0].offset == 0, posVec[0].length == 7
// posVec[1].offset == 0, posVec[1].length == 3
// posVec[2].offset == 4, posVec[2].length == 3
std::string s;
int n = re1.extract("123", s);
// n == 1, s == "123"
n = re1.extract("ab12de", 0, s); // n == 1, s == "12"
n = re1.extract("abcd", 0, s);
// n == 0, s == ""
std::vector<std::string> vec;
re2.split("123 456", 0, vec);
// vec[0] == "123"
// vec[1] == "456"
s = "123";
re1.subst(s, "ABC");
// s == "ABC"
s = "123 456";
re2.subst(s, "$2 $1"); // s == "456 123"
RegularExpression re3("ABC");
RegularExpression re4("ABC", RegularExpression::RE_CASELESS);
match = re3.match("abc", 0); // false
match = re4.match("abc", 0); // true
return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值