[boost.xpressive] RE for replacing problem
Hi, my member function write() get called with string arguments like: Hello \n World \n Traceback ...\n NameError : name ... not defined \n etc. I want to add a prefix (a prompt '>>>' concete) so that the result will become:
Hello World Traceback .... NameError: name ... not defined
Yes, this comes from redirected python output. The simplest solution seems to use regex/xpressive: class redirector { const std::string m_prompt; const xpr::sregex m_re; const std::string m_re_fmt; public: redirector(const std::string& prompt = ">>> ") : m_prompt(prompt), m_re(xpr::sregex::compile("(\\n)!")), m_re_fmt(m_prompt + "\n") { } void write(const std::string& text) { std::cout << m_prompt << xpr::regex_replace(text, m_re, m_re_fmt); } ... }; where my regex won't work. What is the correct one since I can get a single '\n', a terminated '\n' or multiline strings separated by '\n'. Is this approach good for this problem? Anyway, I get a lot of warnings from constructor and write function, any ideas why? Thanks, Olaf
Olaf Peter wrote:
Hi,
my member function write() get called with string arguments like:
Hello \n World \n Traceback ...\n NameError : name ... not defined \n
I'm a little confused. Is there a newline after "Hello", or two?
etc.
I want to add a prefix (a prompt '>>>' concete) so that the result will become:
Hello World Traceback .... NameError: name ... not defined
And how do you know to put "NameError", ":", and "name ... not defined" all on one line, but "Hello" and "World" on different lines?
Yes, this comes from redirected python output.
The simplest solution seems to use regex/xpressive:
class redirector { const std::string m_prompt; const xpr::sregex m_re; const std::string m_re_fmt;
public: redirector(const std::string& prompt = ">>> ") : m_prompt(prompt), m_re(xpr::sregex::compile("(\\n)!")), m_re_fmt(m_prompt + "\n") { }
void write(const std::string& text) {
std::cout << m_prompt << xpr::regex_replace(text, m_re, m_re_fmt); } ... };
where my regex won't work. What is the correct one since I can get a single '\n', a terminated '\n'
What is a "terminated '\n'"?
or multiline strings separated by '\n'. Is this approach good for this problem?
A regex is probably your best bet. If you define the problem better, I or someone else here can probably help. Or, there are plenty of online resources that could help you author the regex. Check out the Regex Coach: http://www.weitz.de/regex-coach/
Anyway, I get a lot of warnings from constructor and write function, any ideas why?
You would need to provide a complete, compilable example and the compiler output, as well as giving me a clue what compiler you're using. -- Eric Niebler BoostPro Computing http://www.boostpro.com
my member function write() get called with string arguments like:
Hello \n World \n Traceback ...\n NameError : name ... not defined \n
I'm a little confused. Is there a newline after "Hello", or two?
one newline; the "Hello" string is '\0' terminated. As next a function call with "\n" is performed, than a "World\0" string followed by a "\n" on next member function call. Note, my member function expect a const std::string& as argument.
etc.
I want to add a prefix (a prompt '>>>' concete) so that the result will become:
Hello World Traceback .... NameError: name ... not defined
And how do you know to put "NameError", ":", and "name ... not defined" all on one line, but "Hello" and "World" on different lines?
The indicator for this is the member function call with "\n". python's output wants a line break and I have to set a prefix prompt on each line. The same with python's "\n" terminated strings I got.
Yes, this comes from redirected python output.
The simplest solution seems to use regex/xpressive:
class redirector { const std::string m_prompt; const xpr::sregex m_re; const std::string m_re_fmt;
public: redirector(const std::string& prompt = ">>> ") : m_prompt(prompt), m_re(xpr::sregex::compile("(\\n)!")), m_re_fmt(m_prompt + "\n") { }
void write(const std::string& text) {
std::cout << m_prompt << xpr::regex_replace(text, m_re, m_re_fmt); } ... };
where my regex won't work. What is the correct one since I can get a single '\n', a terminated '\n'
What is a "terminated '\n'"?
a single means here a function call with "\n" only, terminated means a string with trailing "\n", e.g. the "Traceback ...\n" string.
or multiline strings separated by '\n'. Is this approach good for this problem?
A regex is probably your best bet. If you define the problem better, I or someone else here can probably help. Or, there are plenty of online resources that could help you author the regex. Check out the Regex Coach: http://www.weitz.de/regex-coach/
Anyway, I get a lot of warnings from constructor and write function, any ideas why?
You would need to provide a complete, compilable example and the compiler output, as well as giving me a clue what compiler you're using.
this warnings comes from VS2005 only - I assume that are the 64bit compatibility check or something special. On linux's gcc 3.4 no warnings - even with "-Wall". The problem ask here is a down break of my postings to sig-c++ (http://www.nabble.com/-boost.python--general-questions-td18950884.html) even with regular expression instead boost::tokeniterator. At this time I have more information on how the strings comes from redirected python output ( I did run it inside the debugger ;-) My assumption on this posting about the internal name lookup where wrong. Thanks, Olaf
participants (2)
-
Eric Niebler
-
Olaf Peter