I currently am working on regex in c++ in which I tried to search a substring in a string.
Problem :
String :
<Directory />
AllowOverride none
Require all denied
</Directory>
<Directory "C:/xampp/htdocs/dashboard">
Options +Indexes
AllowOverride None
Require all granted
</Directory>
In this I need only the first Directory contents,that is
<Directory />
AllowOverride none
Require all denied
</Directory>
Hence I used the regex
Regex : < *Directory *\/? *>(\n.*?)+<\/Directory>
In this regex I used a \\n.*? so that it will return the first result(lazy). It works fine when I tried in https://regexr.com but when I use regex_search it shows there isn't a match. How is this possible? Am I missing out anything?
Code :
LPSTR logLocation = "C:\\xampp\\apache\\conf\\httpd.conf";
string logBuffer = RemoveCommentsFromFile(logLocation);
//cout<<logBuffer;
smatch match;
regex regx("< *Directory *\/? *>(\n.*?)+<\/Directory>");
if(regex_search(logBuffer,match,regx))
cout<<match.str();
The code basically removes comments from a file and returns it as string.
After hours of wasting time, I finally found a solution that would fit. There are two regex that would fit in this solution.
Solution 1 : < *Directory */? *>(\\n|.*)+?</Directory>
I used the | (OR) in the capture group with a lazy (?) operator to stop with the first match. However, the regex that I posted in the question seems to be working fine in all regex testers.
Solution 2 : < *Directory */? *>(\\s|\\S)+?</Directory>
Just replaced the newline and any character (.) with \\s and \\S.
However, it is known that (\\s|\\S) is same as [\\s\\S] but it looks like the capture group works but the second one does not.
I don't have any idea how it happens!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.