我有一个.sln文件,我需要从中获取一些数据。 .sln文件看起来像that。我需要这个文件的项目名称。如果它看起来正确的话,我还有一个可以使用带字符串的代码。所以我试着做一个正则表达式
“项目(\”{[\ W - ] +} \ “)\ S * = \ S * \”(\ W +)\ “[,\ S] + \”([\ W \] *。 vcxproj)\ “[,\ S] + \”({[\ W - ] +})\ “”
#include <iostream>
#include <string>
#include <vector>
#include <regex>
#include <fstream>
using namespace std;
int main()
{
ifstream ifstr("C:\\Users\\Andrew\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication1\\ConsoleApplication1.sln");
string all;
//Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "doctor", "dreryk\src\doctor\doctor.vcproj", "{5D031DBA-1903-4067-A2CE-01B104A08D48}"
regex projParse("Project\(\"\{[\w-]+\}\"\)\s*=\s*\"(\w+)\"[,\s]+\"([\w\\]*\.vcxproj)\"[,\s]+\"(\{[\w-]+\})\"");
ifstr.seekg(0);
string pth, sol;
match_results<string::const_iterator> what;
while (getline(ifstr, sol))
{
string::const_iterator start = sol.begin();
string::const_iterator end = sol.end();
if (regex_search(start, end, what, projParse))
{
cout << what[0] << endl;
}
}
}
当我尝试使用此代码时,它表示存在错误。我不知道如何解决它。
错误是:
ConsoleApplication1.exe中0x775EC42D处的未处理异常:Microsoft C ++异常:内存位置0x0031E890处的std :: regex_error。
答案 0 :(得分:1)
所以,我放弃了这一点,我相信我简化了你的正则表达式。这是一些代码:
#include <iostream>
#include <string>
#include <regex>
#include <fstream>
using namespace std;
void show_matches(const std::string& in, const std::string& re)
{
smatch m;
regex_search(in, m, std::regex(re));
if(m.empty()) {
cout << "input=[" << in << "], regex=[" << re << "]: NO MATCH\n";
} else {
cout << "input=[" << in << "], regex=[" << re << "]: ";
cout << "prefix=[" << m.prefix() << "] ";
for(size_t n = 0; n < m.size(); ++n)
cout << " m[" << n << "]=[" << m[n] << "] ";
cout << "suffix=[" << m.suffix() << "]\n";
}
}
int main()
{
show_matches("Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"doctor\", \"dreryk\\src\\doctor\\doctor.vcproj\\\", \"{5D031DBA-1903-4067-A2CE-01B104A08D48}\"",
"Project\\(\"\\{([^\\}]*)\\W+(\\w+)\\W+(.*).vcproj\\W+([^\\}]*)\\W+");
}
正则表达式:
Project\(\"\{([^\}]*)\W+(\w+)\W+(.*).vcproj\W+([^\}]*)\W+
细分:
Project\(\"\{ -- Literally match Project("{
([^\}]*) -- Capture group 1: Capture all characters until }
\W+ -- Eat all non-letter characters until the next capture group
\w+ -- Capture group 2 -- Eat everything until non character (in this case ")
\W+ -- Eat all non-letter characters until we get to our next capture group
(.*).vcproj -- Capture group 3 eat everything until .vcproj
\W -- Eat everything until our last capture group
([^\}]*) -- Capture group 4 - eat everything until }
\W+ Eat until the end of the string.
输入:
Project(\"{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}\") = \"doctor\", \"dreryk\\src\\doctor\\doctor.vcproj\\\", \"{5D031DBA-1903-4067-A2CE-01B104A08D48}\""
输出:
prefix=[] m[0]=[Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "doctor", "dreryk\src\doctor\doctor.vcproj\", "{5D031DBA-1903-4067-A2CE-01B104A08D48}"] m[1]=[8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942] m[2]=[doctor] m[3]=[dreryk\src\doctor\doctor] m[4]=[5D031DBA-1903-4067-A2CE-01B104A08D48] suffix=[]
如果您需要,上面的代码非常适合在C ++中测试正则表达式 - 它来自cppreference.com上的代码(信用在哪里到期)
祝你好运