From 76ed31a29822e40d157792fcbccc604587c3d790 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Fri, 13 May 2022 21:54:39 +0200 Subject: Improve regex in 'CNetAdr2::GetBase' --- NorthstarDedicatedTest/NetAdr2.cpp | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/NorthstarDedicatedTest/NetAdr2.cpp b/NorthstarDedicatedTest/NetAdr2.cpp index edf2d180..1a44390b 100644 --- a/NorthstarDedicatedTest/NetAdr2.cpp +++ b/NorthstarDedicatedTest/NetAdr2.cpp @@ -224,12 +224,18 @@ bool CNetAdr2::SetFromSockadr(sockaddr_storage* s) //----------------------------------------------------------------------------- std::string CNetAdr2::GetBase(void) const { - std::string svIpAdr = m_svip; - static std::regex rx("\\].*"); - svIpAdr.erase(0, 1); - svIpAdr = std::regex_replace(svIpAdr, rx, ""); + static std::regex rx("[^\\[]*.(.*)(\\]).*"); + std::smatch smRegexMatches; + std::regex_search(m_svip, smRegexMatches, rx); - return svIpAdr; + if (smRegexMatches.size() > 0) + { + return smRegexMatches[1].str(); + } + else + { + return "127.0.0.1"; + } } //----------------------------------------------------------------------------- @@ -238,11 +244,18 @@ std::string CNetAdr2::GetBase(void) const //----------------------------------------------------------------------------- std::string CNetAdr2::GetBase(std::string svInAdr) const { - static std::regex rx("\\].*"); - svInAdr.erase(0, 1); - svInAdr = std::regex_replace(svInAdr, rx, ""); + static std::regex rx("[^\\[]*.(.*)(\\]).*"); + std::smatch smRegexMatches; + std::regex_search(svInAdr, smRegexMatches, rx); - return svInAdr; + if (smRegexMatches.size() > 0) + { + return smRegexMatches[1].str(); + } + else + { + return "127.0.0.1"; + } } //----------------------------------------------------------------------------- -- cgit v1.2.3