C++Regex 匹配字符串一直匹配不出来,球帮忙解答下

Stay_Deep 2016-09-14 12:47:04
我想匹配 http://hq.sinajs.cn/list=sh600389
var hq_str_sh600389="江山股份,18.900,19.110,18.800,18.990,18.630,18.800,18.850,882430,16580508.000,12370,18.800,3600,18.780,7450,18.770,2600,18.760,13800,18.750,7000,18.850,600,18.860,1700,18.880,1600,18.900,100,18.910,2016-09-14,09:39:12,00";
里面的 600389 江山股份 以及逗号之间的所有数据,用regex写了半天的正则匹配一直匹配不出来,球高手帮忙写下,谢谢了、

另外帮我看看我写的正则有啥问题

using namespace std;
char RecvDate[250];

send(sinaSocket, SendBuffer, strlen(SendBuffer), 0);
recv(sinaSocket, RecvDate, 250, 0);
/////////////////////////////////////////分析数据包
string RcvDate(RecvDate);
regex Rule1("(^\\d+$)");//匹配600389
regex Rule2("[\u4e00-\u9fa5]"); //匹配中文
regex Rule3("^\d+(\.\d+)?$");//匹配浮点数
smatch m;
smatch n;
smatch o;
string Floats[25];
int i = 0;

regex_search(RcvDate, m, Rule1); //匹配整数
mySinDate.NameNumber = m.str();

regex_search(RcvDate, n, Rule2); //匹配中文
mySinDate.Name = n.str();

while (regex_search(RcvDate, o, Rule3)) //匹配逗号中间的浮点数并且按顺序存储
{
for (auto x = m.begin(); x != m.end(); x++)
{
i++;
Floats[i] = x->str();
}

RcvDate = o.suffix().str();
}



没有分了很抱歉。。。
...全文
1101 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hubin8851 2018-01-26
  • 打赏
  • 举报
回复
\d只是表示转义与自身匹配。还需要加一个“\”变成\\d的形式才能表示匹配某个数字。
Stay_Deep 2016-09-18
  • 打赏
  • 举报
回复
谢谢赵老师!
_明月 2016-09-14
  • 打赏
  • 举报
回复
围观一下,哈哈。
赵4老师 2016-09-14
  • 打赏
  • 举报
回复
仅供参考:
//凡是?。!后面跟1~1000后面跟半角.的,在?。!后面加回车换行。
//in.txt:
//1.测试。2.测试2?3.测试3!4.测试
//四。5.测试。6.测试6?7.测试3!8.测试
//运行该程序将输出重定向到比如out.txt即可将输出保存到文件out.txt中
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
using namespace std;
int main() {
    wifstream wifs("in.txt");
    wifs.imbue(locale("chs"));
    wstring wstr(L""),wln;
    while (wifs) {
        getline(wifs,wln);
        wstr+=wln;
    }
    wifs.close();
    wcout.imbue(locale("chs"));
    wcout << wstr << endl;

    wstring rs = L"([?。!])(\\d{1,3}\\.)";
    wregex expression(rs);
    wstr = regex_replace(wstr, expression, wstring(L"$1\r\n$2"));
    wcout << wstr << endl;

    return 0;
}
//1.测试。2.测试2?3.测试3!4.测试四。5.测试。6.测试6?7.测试3!8.测试
//1.测试。
//2.测试2?
//3.测试3!
//4.测试四。
//5.测试。
//6.测试6?
//7.测试3!
//8.测试
//
赵4老师 2016-09-14
  • 打赏
  • 举报
回复
处理中文Unicode字符串,请使用wchar_t、wstring、wregex、L“字符串内容”、wcout、……
Stay_Deep 2016-09-14
  • 打赏
  • 举报
回复
谢谢赵老师,看了你的方法非常实用,解决了我的问题,另外我还是想问下那个regex到底怎么写?
赵4老师 2016-09-14
  • 打赏
  • 举报
回复
#include <stdio.h>
char s[]="http://hq.sinajs.cn/list=sh600389"
"var hq_str_sh600389=\"江山股份,18.900,19.110,18.800,18.990,18.630,18.800,18.850,882430,16580508.000,12370,18.800,3600,18.780,7450,18.770,2600,18.760,13800,18.750,7000,18.850,600,18.860,1700,18.880,1600,18.900,100,18.910,2016-09-14,09:39:12,00\"";
char t[80];
char *p;
int n,i;
int main() {
    p=s;
    sscanf(p,"http://hq.sinajs.cn/list=sh%6s%n",t,&n);
    printf("[%s]\n",t);
    p+=n;
    sscanf(p,"var hq_str_sh%*6s=\"%[^,],%n",t,&n);
    printf("[%s]\n",t);
    p+=n;
    for (i=0;i<31;i++) {
        sscanf(p,"%[^,],%n",t,&n);
        printf("%02d [%s]\n",i,t);
        p+=n;
    }
    sscanf(p,"%[^\"]",t,&n);
    printf("%02d [%s]\n",i,t);

    return 0;
}
//[600389]
//[江山股份]
//00 [18.900]
//01 [19.110]
//02 [18.800]
//03 [18.990]
//04 [18.630]
//05 [18.800]
//06 [18.850]
//07 [882430]
//08 [16580508.000]
//09 [12370]
//10 [18.800]
//11 [3600]
//12 [18.780]
//13 [7450]
//14 [18.770]
//15 [2600]
//16 [18.760]
//17 [13800]
//18 [18.750]
//19 [7000]
//20 [18.850]
//21 [600]
//22 [18.860]
//23 [1700]
//24 [18.880]
//25 [1600]
//26 [18.900]
//27 [100]
//28 [18.910]
//29 [2016-09-14]
//30 [09:39:12]
//31 [00]
//

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧