// ------------------------------------------------------------------- StringArray PosixRegEx::split(const String &str, bool empty, int eflags) { if( !compiled) { BLOCXX_THROW(RegExCompileException, "Regular expression is not compiled"); } MatchArray rsub; StringArray ssub; bool match; size_t off = 0; size_t len = str.length(); do { match = execute(rsub, str, off, 1, eflags); if( match) { if( rsub.empty() || rsub[0].rm_so < 0 || rsub[0].rm_eo < 0) { BLOCXX_THROW(RegExCompileException, "Non-capturing regular expression"); } if( empty || ((size_t)rsub[0].rm_so > off)) { ssub.push_back(str.substring(off, rsub[0].rm_so - off)); } off = rsub[0].rm_eo; } else if(m_ecode == REG_NOMATCH) { String tmp = str.substring(off); if( empty || !tmp.empty()) { ssub.push_back(tmp); } m_ecode = REG_NOERROR; m_error.erase(); } else { BLOCXX_THROW_ERR(RegExExecuteException, errorString().c_str(), m_ecode); } } while(match && len > off); return ssub; }
// ------------------------------------------------------------------- blocxx::String PosixRegEx::replace(const String &str, const String &rep, bool global, int eflags) { if( !compiled) { BLOCXX_THROW(RegExCompileException, "Regular expression is not compiled"); } MatchArray rsub; bool match; size_t off = 0; String out = str; do { match = execute(rsub, out, off, 0, eflags); if( match) { if( rsub.empty() || rsub[0].rm_so < 0 || rsub[0].rm_eo < 0) { // only if empty (missused as guard). BLOCXX_THROW(RegExCompileException, "Non-capturing regular expression"); } String res = substitute_caps(rsub, out, rep); out = out.substring(0, rsub[0].rm_so) + res + out.substring(rsub[0].rm_eo); off = rsub[0].rm_so + res.length(); } else if(m_ecode == REG_NOMATCH) { m_ecode = REG_NOERROR; m_error.erase(); } else { BLOCXX_THROW_ERR(RegExExecuteException, errorString().c_str(), m_ecode); } } while(global && match && out.length() > off); return out; }
// ------------------------------------------------------------------- StringArray PosixRegEx::capture(const String &str, size_t index, size_t count, int eflags) { if( !compiled) { BLOCXX_THROW(RegExCompileException, "Regular expression is not compiled"); } MatchArray rsub; StringArray ssub; bool match = execute(rsub, str, index, count, eflags); if( match) { if( rsub.empty()) { BLOCXX_THROW(RegExCompileException, "Non-capturing regular expression"); } MatchArray::const_iterator i=rsub.begin(); for( ; i != rsub.end(); ++i) { if( i->rm_so >= 0 && i->rm_eo >= 0) { ssub.push_back(str.substring(i->rm_so, i->rm_eo - i->rm_so)); } else { ssub.push_back(String("")); } } } else if(m_ecode != REG_NOMATCH) { BLOCXX_THROW_ERR(RegExExecuteException, errorString().c_str(), m_ecode); } return ssub; }