sscanf解析方括号内的值

时间:2016-06-06 15:08:43

标签: c++

我正在尝试解析方括号内的值:

MYVAR [3]

std::string key("MYVAR[3]");

int idx;
int result = sscanf(key.c_str(), "%*s[%i]", &idx);
if (result > 0)
{
    std::cout << "It's array value" << std::endl;
}

我该怎么做?

2 个答案:

答案 0 :(得分:3)

%*s会读取字符[,因此您应该使用说明符"%*[^[][%i]"在字符前停止。

答案 1 :(得分:1)

"MYVAR[3]"int result = sscanf(key.c_str(), some_format, &idx); if (result > 0) 都会提供相同的结果。

sscanf()

IOWs,整数之后的内容不相关/未选中,因为"%n"不用于解析方括号内的

代码应检查两个括号是否存在。

存在许多方法。建议使用#define FMT_NOT_LBRACKET "%*[^[]" #define FMT_BRACKETED_INT "[%i]" int idx; int n = 0; sscanf(key.c_str(), FMT_NOT_LBRACKET FMT_BRACKETED_INT "%n", &idx, &n); if (n) { Success(); } 检测扫描完成情况。

install.packages("sendmailR")
library(sendmailR)
sender <- "myemailid@umd.edu"
recipients <- "myemailid@gmail.com"
send.mail(from = sender,
      to = recipients,
      subject = "Subject of the email",
      body = "Body of the email",
      smtp = list(host.name = "exch.mail.umd.edu", port = 465,
                  user.name = "myemailid@umd.edu",            
                  passwd = "mypassword", ssl = TRUE),
      authenticate = TRUE,
      send = TRUE)