3
\$\begingroup\$

I've written in a C++ dll the following function to return to VBA a substring containing a certain pattern, given an input string, which contains that substring:

VARIANT _stdcall ReturnT(VARIANT FileName)
{
 wstring str(FileName.bstrVal, SysStringLen(FileName.bstrVal));
 wregex ToFind(L"_[tT]\\d{2,3}(_|.)");
 wsmatch TStr;
 regex_search(str, TStr, ToFind);
 wstring TVal = TStr.str(0).erase(0, 2);
 TVal.erase(TVal.end() - 1);
 assert(!TVal.empty());
 BSTR bs = SysAllocStringLen(TVal.data(), TVal.size());
 VARIANT TValue;
 TValue.vt = VT_BSTR;
 TValue.bstrVal = bs;
 return TValue;
}

This works flawlessly, but I suspect I am doing something more complicated than it really should be.

Notice that I'm using VARIANT to avoid interop problems ( BSTR as input would return kanjis, and in output would return a string with spaces between the characters)

t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
asked May 10, 2017 at 6:38
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

String Operations

regex_search provides various overloads for various input types. You don't need to copy the data into a wstring just to pass it to regex_search.

Since the return string is only a substring of the original, you can simply construct the BSTR from part of the match value, avoiding the erase calls.

ATL

Microsoft's ATL library provides a lot of helper classes for dealing with COM. They can eliminate a lot of boilerplate code.

Regex Library

I haven't used VBA lately but I expect something as common as regular expressions to already be available. If not, I would probably expose COM classes wrapping regex functionality to VBA and keep the actual expressions in VBA closer to the rest of the business code.

answered May 10, 2017 at 17:54
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.