c++如何识别指定字符串?

图片说明
如图所示,我要识别“Elements=3150”,得到3150这个数字,然后在跳转两行之后读取下面的数字(场)信息输入到指定数组中。

VS2017,C++11,如图图片说明

 正则表达式 (?is)Elements=\d+.*\n.*\n

至于如何使用正则:https://blog.csdn.net/tojohnonly/article/details/78326633

#include

const TCHAR * tcsStart;
const TCHAR * tcsEnd;
TCHAR tcsNewRule[1024] = _T("");
TCHAR tcsPrefix[MAX_PATH] = _T("");
_tcscpy(tcsNewRule, _T("xxxxddhiadhwElements=3150,jsijedijeij"));
CAtlRegExp<> reNewRule;
REParseError status = reNewRule.Parse(_T("Elements={.*},"));
if (REPARSE_ERROR_OK != status)
{
return;
}
//匹配出前缀和后缀
CAtlREMatchContext<> mcNewRule;
if (!reNewRule.Match(tcsNewRule, &mcNewRule))
{
return;
}
else
{
if(mcNewRule.m_uNumGroups > 0)
{
mcNewRule.GetMatch(0, &tcsStart, &tcsEnd);
_tcsncpy_s(tcsPrefix, tcsStart, tcsEnd - tcsStart);
}
}

以上代码我自己测试通过了
得到3150

使用正则表达式 (?<=Elements=)\d+
详细可见
http://www.runoob.com/regexp/regexp-metachar.html

在C语言中,字符串存储为字符数组,以'\0'结束。 在C的接口中,有strstr函数,可以在字符串中查找另一个字符串。

char * strstr(const char *str1, const char *str2);

功能为在str1中查找str2,如果存在,那么返回查找到的起始指针,否则返回NULL。

参考代码:
#include
#include
#include
using namespace std;
int main()
{
string a="abcdefghigklmn";
char *b="def";
char *c="123";

if(strstr(a.c_str(), b) == NULL)//在a中查找b,如果不存在,
    cout << "not found\n";//输出结果。
else//否则存在。
    cout <<"found\n"; //输出结果。
if(strstr(a.c_str(), c) == NULL)//在a中查找b,如果不存在,
    cout << "not found\n";//输出结果。
else//否则存在。
    cout <<"found\n"; //输出结果。
return 0;

}

直接strstr搜也可以,当然正则表达式更高端一点,其实就一步步操作就行
strstr找到Elements=然后读取后面的数字,然后在找到两个\n之后就是你要的内容了

使用strstr 查找Elements,然后通过fgets跳过两行,接着fread就行

for (i=0;i if (s[i]>='0'&&s[i]<='9') s1+=s[i];
s代表要识别的字符串;
s1储存结果