This is a homework assignment. OP wrote “Example” when he should have written “Test”.
The point is to transform each element of the
J
string to an email address.
Hence, “Jowal Dawn” ⟶ “Jowal Dawn <jowal.dawn@test.com>”.
There is no need to
validate anything. You need to implement the function
solve
, which breaks the string into individual names, transforms those names into email addresses, and builds a new string with those parts.
(IRL, it is only possible to validate an email address by getting a response from someone you emailed.)
Don’t try to do
everything with the regular expression. Only use it to get the pieces. C++ has a
regex_iterator
which will allow you to handle each name in
J
one at a time. All you need is a loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
std::string solve( std::string & J, std::string & K )
{
std::string result;
std::regex re( ... ); // this is where you need to think
auto first = std::regex_iterator( J.begin(), J.end(), re );
auto last = std::regex_iterator();
for (auto iter = first; iter != last; ++iter)
{
std::smatch match = *iter;
// Use the pieces of `match` to build your new piece of string:
auto first_name = ...;
auto last_name = ...;
if (!result.empty()) result += "; ";
result += first_name + " " + last_name
+ " <" + lowercase(first_name) + "." + lowercase(last_name)
+ "@" + lowercase(K) + ".com>";
}
return result;
}
|
Notice that you will need a little utility function (
lowercase()
) to convert a string to lowercase.
The tricky part is designing your regular expression. Remember: you can get
sub-expressions by using parentheses in the regular expression. The
match
variable can be treated like an array to access those sub-matches.
match[0]
is the
entire matched expression
match[1]
is the first parenthesized
sub-expression matched
... and so on
Also remember that you can tag something as optional with a question mark.
one;?
— the
;
is optional.
Finally, remember that the regular expression matches the
input string. It does not match (or create) the result string.
Good luck!