I am trying to parse a certificate in c++ and decided it was a good opportunity to learn Regex. I just learned about regex an hour or so ago, so excuse my lack of knowledge.
I am looking for all of the OU's associated with an entry.
I am doing the following:
std::smatch OuMatches;
std::string myCertSubject = "O=\"My Company, Incorporated\", OU=Technician Level - A3, OU=Access Level - 1, CN=\"Name, My\", [email protected]";
std::regex subjectRx("OU=[[:w:]|[:s:]|[:digit:]|-]*", std::regex_constants::icase);
bool foundOU = std::regex_search(mySubject,OuMatches,subjectRx);
Why won't this give me all of the results (2) that match my reg ex? Is there a way to get this?
It looks like you're just trying to get a string that looks like OU=XXXXXXXXXXXXXXXXX
followed by a comma or a semicolon.
This regex will do that:
OU=[^,;]+
What this means is the string OU=
, followed by at least one character that isn't a comma or semicolon:
[^,;]+
Here's a code sample using this regex to print the matches (based on the example here):
std::smatch OuMatches;
std::string myCertSubject = "O=\"My Company, Incorporated\", OU=Technician Level - A3, OU=Access Level - 1, CN=\"Name, My\", [email protected]";
std::regex subjectRx("OU=[^,;]+", std::regex_constants::icase);
std::regex_iterator<std::string::iterator> it (myCertSubject.begin(), myCertSubject.end(), subjectRx);
std::regex_iterator<std::string::iterator> end;
while (it != end)
{
std::cout << it->str() << std::endl;
++it;
}
Try using a negated character class instead. I get the feeling your character classes aren't behaving like you think they are...
subjectRx("OU=[^,]*", std::regex_constants::icase);
[^,]*
will match all characters except a comma.
As for the matches, try using a loop:
while (std::regex_search (mySubject,OuMatches,subjectRx)) {
// do something
}
I don't know much C++, but I found this documentation page which I think should be a bit more useful.
The piece of code it has here is
while (std::regex_search (s,m,e)) {
for (auto x:m) std::cout << x << " ";
std::cout << std::endl;
s = m.suffix().str();
}
EDIT: I just realise that you can have commas in the parameters like in O=
, which won't be working with [^,]
. Instead, you can use this regex:
OU=(?:[^,]|,(?!(?:[^"]*"[^"]*"[^"]*)*$))*
You can see an example with O=
here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With