CString获取数字,用正则最好,该如何处理
CString获取数字,用正则最好
CString现在获取html的一个table 如下: 我想提取里面的数据,6.9785 6.9785, 0.1178,0.1178 怎么提取,正则怎么写
<TABLE ALIGN=LEFT>
<TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A0'>6.9785
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_0'>6.9785
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A1'>0.1178
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_1'>0.1178
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A2'>0.3122
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_2'>0.3122
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A3'>0.0908
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_3'>0.0908
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A4'>0.0213
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_4'>0.0213
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A5'>0.0015
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_5'>0.0015
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A6'>0.0034
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_6'>0.0034
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A7'>0.0049
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_7'>0.0049
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A8'>0.0011
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_8'>0.0011
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A9'>0.0000
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_9'>0.0000
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A10'>0.0024
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_10'>0.0024
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A11'>0.0044
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_11'>0.0044
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A12'>0.0005
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_12'>0.0005
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A13'>92.5248
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_13'>92.5248
</SPAN>
</TABLE>
<INPUT TYPE=HIDDEN NAME=ep VALUE=''>
</FORM>
</BODY>
</HTML>
------解决思路----------------------
假设工程字符集为Unicode,示例代码为:
需要在VS2010或以上的编译器中编译,否则请使用boost的regex
------解决思路----------------------
把保存着html的CString对象作为参数调用GetDataFromHtml,返回值是一个字符串数组,保存着所有的结果
CString现在获取html的一个table 如下: 我想提取里面的数据,6.9785 6.9785, 0.1178,0.1178 怎么提取,正则怎么写
<TABLE ALIGN=LEFT>
<TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A0'>6.9785
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_0'>6.9785
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A1'>0.1178
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_1'>0.1178
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A2'>0.3122
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_2'>0.3122
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A3'>0.0908
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_3'>0.0908
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A4'>0.0213
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_4'>0.0213
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A5'>0.0015
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_5'>0.0015
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A6'>0.0034
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_6'>0.0034
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A7'>0.0049
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_7'>0.0049
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A8'>0.0011
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_8'>0.0011
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A9'>0.0000
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_9'>0.0000
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A10'>0.0024
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_10'>0.0024
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A11'>0.0044
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_11'>0.0044
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A12'>0.0005
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_12'>0.0005
</SPAN><TR ALIGN=RIGHT>
<TD WIDTH=59 CLASS=hgwavg NOWRAP><SPAN ID='A13'>92.5248
</SPAN><TD WIDTH=59 CLASS=hgwman NOWRAP><SPAN ID='R0_13'>92.5248
</SPAN>
</TABLE>
<INPUT TYPE=HIDDEN NAME=ep VALUE=''>
</FORM>
</BODY>
</HTML>
------解决思路----------------------
假设工程字符集为Unicode,示例代码为:
#include <regex>
#include <string>
#include <vector>
using namespace std;
vector<CString> GetDataFromHtml(const wchar_t *html)
{
wregex pattern(L"<SPAN ID='A\\d+'>([\\d\\.]+?)\\s*?</SPAN>");
vector<CString> results;
wsmatch match;
wstring html_data = html;
while (regex_search(html_data, match, pattern))
{
results.push_back(match[1].str().c_str());
html_data = match.suffix();
}
return results;
}
需要在VS2010或以上的编译器中编译,否则请使用boost的regex
------解决思路----------------------
把保存着html的CString对象作为参数调用GetDataFromHtml,返回值是一个字符串数组,保存着所有的结果