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

  • Thread starter TylerH
  • Start date
In summary, the conversation discusses how to require a character without having it reported in a sub_match. The solution involves using regular expressions with specific syntax and ensuring that the +s are outside the groupings in parentheses. The conversation also mentions using Boost's regex and Perl syntax. One of the participants offers to help with a code snippet to demonstrate the solution. The conversation concludes with a request for suggestions on how to better add expressions in C++.
  • #1
TylerH
729
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
  • #2


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.
 
  • #3


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

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

What does it mean to "require a character"?

Requiring a character means that a specific character must be present in a certain context or situation in order for the desired outcome to occur.

Can you give an example of requiring a character?

For example, if you want to require a user to enter a valid email address in a form, you would use a character such as "@" to ensure that the user has inputted a valid email address.

How can I require a character without it being reported in the output?

This can be achieved by using a special character called a "non-capturing group" in regular expressions. This tells the program to match the required character but not include it in the final output.

What is the purpose of requiring a character but not reporting it?

Requiring a character without reporting it can be useful in situations where you want to validate user input without including it in the final result. This can be helpful for security purposes or when dealing with sensitive information.

Are there any other ways to require a character without reporting it?

Yes, in addition to using non-capturing groups in regular expressions, you can also use lookaheads or lookbehinds to require a character without it being included in the output. These are advanced techniques that can be useful in certain scenarios.

Similar threads

  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
715
Back
Top