c++;正则表达式未按预期工作(正则表达式搜索) 我目前在C++中使用正则表达式,在其中尝试在字符串中搜索子串。< /P>
问题: 字符串:c++;正则表达式未按预期工作(正则表达式搜索) 我目前在C++中使用正则表达式,在其中尝试在字符串中搜索子串。< /P>,c++,regex,c++11,C++,Regex,C++11,问题: 字符串: <Directory /> AllowOverride none Require all denied </Directory> <Directory "C:/xampp/htdocs/dashboard"> Options +Indexes AllowOverride None Require all granted </Directory> 不允许超限 要求全部拒绝 选项+索引 不允许超限 要求所有授权 在这里,我只需
<Directory />
AllowOverride none
Require all denied
</Directory>
<Directory "C:/xampp/htdocs/dashboard">
Options +Indexes
AllowOverride None
Require all granted
</Directory>
不允许超限
要求全部拒绝
选项+索引
不允许超限
要求所有授权
在这里,我只需要第一个目录内容,即
<Directory />
AllowOverride none
Require all denied
</Directory>
不允许超限
要求全部拒绝
因此我使用正则表达式
Regex : < *Directory *\/? *>(\n.*?)+<\/Directory>
Regex:<*目录*\/?*>(\n.*+
在这个正则表达式中,我使用了一个\n.,这样它将返回第一个结果(lazy)。当我尝试进入时,它工作正常,但当我使用regex_搜索时,它显示没有匹配项。这怎么可能?我遗漏了什么吗
代码:
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();
LPSTR logLocation=“C:\\xampp\\apache\\conf\\httpd.conf”;
字符串logBuffer=RemoveCommentsFromFile(logLocation);
//经过几个小时的浪费时间,我终于找到了一个合适的解决方案。
有两个正则表达式适用于此解决方案
Solution 1 : < *Directory */? *>(\\n|.*)+?</Directory>
解决方案1:<*目录*/?*>(\\n |*)+?
我使用捕获组中的|(OR)和惰性(?)操作符停止第一个匹配。然而,我在问题中发布的正则表达式似乎在所有正则表达式测试人员中都运行良好
Solution 2 : < *Directory */? *>(\\s|\\S)+?</Directory>
解决方案2:<*目录*/?*>(\\s |\\s)+?
仅将换行符和任何字符(.)替换为\s和\s
但是,已知(\s |\s)与[\s\s]相同,但看起来捕获组可以工作,但第二个组不能
我不知道这是怎么发生的 你确定你的换行符是LF吗?请尝试<*目录*/?*>(?:\r?\n.*?
以匹配LF之前存在的可选CR。yeah尝试strstr并检查其中是否存在CR,结果返回false。请提供代码段以重新编写。将其转换为regex regx(r)(<*目录*\/?*>(\n.*))代码>我现在无法测试它,但是您不需要在\n`(如\\n
)中退出`in吗?