adding space before and after a specific word using regex

how would I add space before and after a specific word in a string?
eg user inputs 5s5s4>5 if I want to change the string into 5 s 5 s 4 > 5 how would I do achieve using regex?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <regex>

std::string add_spaces( const std::string& text, const std::regex& re )
{
    // " $& " - space, a copy of the match, space
    return std::regex_replace( text, re, " $& " ) ;
}

int main()
{
    std::cout << '"' << add_spaces( "abcdxyzefghxyzijklxyzmnop", std::regex("xyz") ) << "\"\n" ;
}

http://coliru.stacked-crooked.com/a/ffa501db2311871d
@JLBorges surprisingly simple
Last edited on
Registered users can post here. Sign in or register to post.