mirror of
https://github.com/pocoproject/poco.git
synced 2024-12-17 03:55:59 +01:00
Named substring support
This commit is contained in:
parent
80c987c9ef
commit
8f1f678f76
@ -25,6 +25,7 @@
|
||||
|
||||
#include "Poco/Foundation.h"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
|
||||
//
|
||||
@ -91,8 +92,10 @@ public:
|
||||
{
|
||||
std::string::size_type offset; /// zero based offset (std::string::npos if subexpr does not match)
|
||||
std::string::size_type length; /// length of substring
|
||||
std::string name; /// name of group
|
||||
};
|
||||
typedef std::vector<Match> MatchVec;
|
||||
typedef std::map<int, std::string> GroupMap;
|
||||
|
||||
RegularExpression(const std::string& pattern, int options = 0, bool study = true);
|
||||
/// Creates a regular expression and parses the given pattern.
|
||||
@ -212,6 +215,8 @@ private:
|
||||
pcre* _pcre;
|
||||
pcre_extra* _extra;
|
||||
|
||||
GroupMap _groups;
|
||||
|
||||
static const int OVEC_SIZE;
|
||||
|
||||
RegularExpression();
|
||||
|
@ -24,7 +24,6 @@
|
||||
#include "pcre.h"
|
||||
#endif
|
||||
|
||||
|
||||
namespace Poco {
|
||||
|
||||
|
||||
@ -35,6 +34,10 @@ RegularExpression::RegularExpression(const std::string& pattern, int options, bo
|
||||
{
|
||||
const char* error;
|
||||
int offs;
|
||||
unsigned nmcount;
|
||||
unsigned nmentrysz;
|
||||
unsigned char* nmtbl;
|
||||
|
||||
_pcre = pcre_compile(pattern.c_str(), options, &error, &offs, 0);
|
||||
if (!_pcre)
|
||||
{
|
||||
@ -44,6 +47,18 @@ RegularExpression::RegularExpression(const std::string& pattern, int options, bo
|
||||
}
|
||||
if (study)
|
||||
_extra = pcre_study(_pcre, 0, &error);
|
||||
|
||||
pcre_fullinfo(_pcre, _extra, PCRE_INFO_NAMECOUNT, &nmcount);
|
||||
pcre_fullinfo(_pcre, _extra, PCRE_INFO_NAMEENTRYSIZE, &nmentrysz);
|
||||
pcre_fullinfo(_pcre, _extra, PCRE_INFO_NAMETABLE, &nmtbl);
|
||||
|
||||
for (int i = 0; i < nmcount; i++)
|
||||
{
|
||||
unsigned char* group = nmtbl + 2 + (nmentrysz * i);
|
||||
int n = pcre_get_stringnumber(_pcre, (char*) group);
|
||||
_groups[n] = std::string((char*) group);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -116,8 +131,17 @@ int RegularExpression::match(const std::string& subject, std::string::size_type
|
||||
for (int i = 0; i < rc; ++i)
|
||||
{
|
||||
Match m;
|
||||
GroupMap::const_iterator it;
|
||||
|
||||
m.offset = ovec[i*2] < 0 ? std::string::npos : ovec[i*2] ;
|
||||
m.length = ovec[i*2 + 1] - m.offset;
|
||||
|
||||
it = _groups.find(i);
|
||||
if (it != _groups.end())
|
||||
{
|
||||
m.name = (*it).second;
|
||||
}
|
||||
|
||||
matches.push_back(m);
|
||||
}
|
||||
return rc;
|
||||
|
Loading…
Reference in New Issue
Block a user