Sha256: bf71e4a59e243a9c55d169de0f3d9f08919391ac01d42d8bca4d28b6a8bd9e52

Contents?: true

Size: 1.63 KB

Versions: 10

Compression:

Stored size: 1.63 KB

Contents

//
//  RegexMatch.cc
//  snowcrash
//
//  Created by Zdenek Nemec on 7/1/13.
//  Copyright (c) 2013 Apiary Inc. All rights reserved.
//

#include <regex>
#include <cstring>
#include "RegexMatch.h"

using namespace std;

#if _MSC_VER == 1500
using namespace std::tr1;
#endif
//
// A C++09 implementation
//

bool snowcrash::RegexMatch(const string& target, const string& expression)
{
    if (target.empty() || expression.empty())
        return false;
    
    try {
        regex pattern(expression, regex_constants::extended);
        return regex_search(target, pattern);
    }
    catch (const regex_error&) {
    }
    catch (...) {
    }
    
    return false;
}

string snowcrash::RegexCaptureFirst(const string& target, const string& expression)
{
    CaptureGroups groups;
    if (!RegexCapture(target, expression, groups) ||
        groups.size() < 2)
        return string();
    
    return groups[1];
}

bool snowcrash::RegexCapture(const string& target, const string& expression, CaptureGroups& captureGroups, size_t groupSize)
{
    if (target.empty() || expression.empty())
        return false;
    
    captureGroups.clear();

    try {
        
        regex pattern(expression, regex_constants::extended);
        match_results<string::const_iterator> result;
        if (!regex_search(target, result, pattern))
            return false;
    
        for (match_results<string::const_iterator>::const_iterator it = result.begin();
             it != result.end();
             ++it) {

            captureGroups.push_back(*it);
        }

        return true;
    }
    catch (const regex_error&) {
    }
    catch (...) {
    }
    
    return false;
}

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
redsnow-0.2.1 ext/snowcrash/src/win/RegexMatch.cc
redsnow-0.2.0 ext/snowcrash/src/win/RegexMatch.cc
redsnow-0.1.6 ext/snowcrash/src/win/RegexMatch.cc
redsnow-0.1.5 ext/snowcrash/src/win/RegexMatch.cc
redsnow-0.1.4 ext/snowcrash/src/win/RegexMatch.cc
redsnow-0.1.3 ext/snowcrash/src/win/RegexMatch.cc
redsnow-0.1.2 ext/snowcrash/src/win/RegexMatch.cc
redsnow-0.1.1 ext/snowcrash/src/win/RegexMatch.cc
redsnow-0.1.0 ext/snowcrash/src/win/RegexMatch.cc
redsnow-0.0.8 ext/snowcrash/src/win/RegexMatch.cc