views:

117

answers:

2

with "basic" is meant: Only the operators "+" (->following..) and "|" (->or) are needed.

Prototype:

preg_match_all(std::string pattern, std::string subject, std::vector<std::string> &matches)

Usage Example:

std::vector<std::string> matches;
std::string pattern, subject;
subject = "Some text with a lots of foo foo and " +  char(255) + " again " + char(255);
pattern = "/" + char(255) + char(255) + "+|foo+/";
preg_match_all(pattern, subject, matches);

The matches should be available afterwardsa via matches[n]. Someone got a hint without using boost and/or PCRE? If not, how I got this realized with boost?

A: 

You could rollup something using std::string::find, doing matches via a functor, and pushing the results onto a string vector.

The way it's realized in boost is probably overkill for what you want -- you first would need to decompose the expression into lexems and then compile a state machine for parsing the given regexp.

Kornel Kisielewicz
thank you for your answer! maybe it is smarter - cause I can control how the pattern is being created - to put the pattern into a vector, too.I'll try something like this: void match_all(std::vector<std::string> pattern, std::string subject, std::vector<std::string> for (unsigned long int i = 0; i < patKey; i++) { //do some find + push into matches... } }
Georg
@Georg - not a bad idea would be to add a functor to define the matching.
Kornel Kisielewicz
+1  A: 

Look into Boost.Regex, http://www.boost.org/doc/libs/1_41_0/libs/regex/doc/html/index.html

dauphic
Woah is this the answer to "Someone got a hint without using boost and/or PCRE? If not, how I got this realized with boost?"?
Kornel Kisielewicz
Every project should have Boost included unless there's a very good reason not to, so there's not much reason to not use Boost where possible; I firmly believe any C++ programmer who opts to reinvent something Boost already does, without good reason, should be sacked instantly, so nobody has to deal with their code. The asker's example suggested they want to be able to pass a pattern, so I think Boost.Regex is the best choice.
dauphic
@dauphic, what you wrote however was " Q: what can I use to mimic Boost.Regex? A: Boost.Regex " -- I wouldn't consider that a useful answer.
Kornel Kisielewicz
@dauphic, as for using boost everywhere, I happen to work on fiscal embedded devices, you can't use dynamicaly allocated memory, nor exceptions there, due to the limits of the hardware -- will you still tell me "use boost"? Also, I've been working for Nokia writing a massive cell-based server -- all libraries used must have been accepted by Nokia. We asked for boost, reply "No". And no further negotiations were possible.
Kornel Kisielewicz
That's a special situation and most definitely NOT the norm. For the majority of C++ programmers, most likely including this one judging by it's other questions, there's absolutely no reason not to use Boost. Especially due to the fact it seems to come from a perl background; you don't jump from perl to embedded systems in C++.
dauphic
@dauphic, "use boost" may sound nice when learning, but the Real World has it's limits, and you ought to learn to be able to live without it.
Kornel Kisielewicz
Also note that the asker's last sentence. This implies that they CAN use Boost, but are choosing NOT to use Boost. Not using Boost where possible shouldn't be encouraged in any way. Reinventing the wheel is not an acceptable practice.
dauphic
@dauphic - Nokia policy states boost as an "immature library", simmilary to many other bigger software vendors. While I personally may disagree about it's immatureness, in the corporate world nobody gives a damn.
Kornel Kisielewicz
I honestly haven't seen any employers I've applied to taking that stance; I'd instantly turn a job offer down if Boost wasn't used, unless there were hardware constraints. However, Boost.Regex and many of the other must-have Boost libraries are being included in the next standard, so it doesn't really matter. Like I said, I don't think any programmers should be encouraged to ever reinvent Boost or standard library functionality.
dauphic