How do I require a character but not have it reported in a

  • Thread starter Thread starter TylerH
  • Start date Start date
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 2K views
TylerH
Messages
729
Reaction score
0
How do I require a character but not have it reported in a sub_match?

Specifically:
Code:
void expression::add(expression &e, const expression &f)
{
	regex arbitrary("(^|\\+)\\s*\\l+(\\((\\d+|\\l+)\\)|)\\s*(\\+|$)");
	smatch matches;
	std::string r, t;
	
	t = e.e + f.e;
	
	regex_search(t, matches, arbitrary);
	for(auto match = matches.begin();match < matches.end();match++)
	{
		std::cout << *match << std::endl;
		r += *match;
	}
	
	e.e = r;
}
How do I get arbitrary to match all the additive terms of a mathematical expression, but keep the +s out of the sub_match-es?

BTW, I'm using Boost's regex, Perl syntax.

Thanks for reading.
 
Physics news on Phys.org


Can you not just make sure the +s are outside the groupings in parentheses? Also note that the first match should be the original string, so you probably are interested only in the following ones. Might want to iterate with a for loop starting at index 1.

matches[0].first would be the starting iterator and matches[0].second would be the end iterator for the original string. Then matches[1].first and matches[1].second would be the begin/end iterators for the first subexpression, etc.

Mind you, I'm rusty on boost:regex. That and reading other people's regex expressions makes my eyes cross. Heck, reading my own a few months later does the same. :biggrin:

If you PM me a self contained code snippet I can compile and mess with (along with an idea of the expected output), I can see if I can get it going.
 


Wow, it's been a while. Sorry, I've been distracted.

I've lost the code since I posted this. I see, from your response, that my attempt is far from optimal. How would you go about adding expressions in C++?