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

  • Thread starter Thread starter TylerH
  • Start date Start date
Click For Summary
SUMMARY

The discussion centers on using Boost's regex library in C++ to match additive terms in mathematical expressions while excluding the '+' characters from sub-matches. The user seeks to refine their regex pattern to achieve this goal, specifically using the Perl syntax for regex. Key suggestions include ensuring '+' characters are outside of parentheses groupings and utilizing the correct match iterators to access sub-expressions. The conversation highlights the importance of regex optimization and clarity in expression handling.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with Boost libraries, specifically Boost.Regex
  • Knowledge of regular expressions, particularly Perl syntax
  • Basic concepts of mathematical expressions and parsing
NEXT STEPS
  • Explore advanced Boost.Regex features for complex pattern matching
  • Learn about regex performance optimization techniques
  • Investigate C++ expression templates for mathematical expression handling
  • Review best practices for writing and maintaining regex patterns
USEFUL FOR

Software developers, particularly those working with C++ and regex, as well as anyone involved in parsing mathematical expressions or optimizing regex performance.

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.
 
Technology 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++?
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
7K
Replies
1
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
5K