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

  • Thread starter Thread starter TylerH
  • Start date Start date
AI Thread Summary
The discussion centers on how to construct a regex pattern in C++ using Boost's regex library to match additive terms in a mathematical expression while excluding the plus signs from the sub-matches. The user seeks to refine their regex pattern to ensure that the plus signs are not included in the captured groups. Suggestions include adjusting the regex to place the plus signs outside the parentheses to avoid them being captured in the sub-matches. It is also noted that the first match should represent the entire string, with subsequent matches corresponding to individual sub-expressions. Additionally, there is an invitation to share a self-contained code snippet for further testing and refinement. The user acknowledges a lack of recent experience with Boost's regex and expresses a desire for clarity and assistance in optimizing their approach to adding expressions in C++.
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++?
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top