- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我的部分代码
bool CSettings::bParseLine ( const char* input )
{
//_asm INT 3
std::string line ( input );
std::size_t position = std::string::npos, comment;
regex cvarPattern ( "\\.([a-zA-Z_]+)" );
regex parentPattern ( "^([a-zA-Z0-9_]+)\\." );
regex cvarValue ( "\\.[a-zA-Z0-9_]+[ ]*=[ ]*(\\d+\\.*\\d*)" );
std::cmatch matchedParent, matchedCvar;
if ( line.empty ( ) )
return false;
if ( !std::regex_match ( line.c_str ( ), matchedParent, parentPattern ) )
return false;
if ( !std::regex_match ( line.c_str ( ), matchedCvar, cvarPattern ) )
return false;
...
}
我尝试将我从文件中读取的行与它分开 - 行看起来像:
foo.bar = 15
baz.asd = 13
ddd.dgh = 66
我想从中提取部分 - 例如对于第一行 foo.bar = 15,我想以这样的方式结束:
a = foo
b = bar
c = 15
但是现在,regex 总是返回 false,我在许多在线 regex 检查器上测试过它,甚至在 visual studio 中,它运行良好,我需要一些不同的 C++ regex_match 语法吗?我正在使用 visual studio 2013 社区
最佳答案
问题是 std::regex_match必须匹配整个字符串,但您试图只匹配其中的一部分。
您需要使用 std::regex_search或更改您的正则表达式以同时匹配所有三个部分:
#include <regex>
#include <string>
#include <iostream>
const auto test =
{
"foo.bar = 15"
, "baz.asd = 13"
, "ddd.dgh = 66"
};
int main()
{
const std::regex r(R"~(([^.]+)\.([^\s]+)[^0-9]+(\d+))~");
// ( 1 ) ( 2 ) ( 3 ) <- capture groups
std::cmatch m;
for(const auto& line: test)
{
if(std::regex_match(line, m, r))
{
// m.str(0) is the entire matched string
// m.str(1) is the 1st capture group
// etc...
std::cout << "a = " << m.str(1) << '\n';
std::cout << "b = " << m.str(2) << '\n';
std::cout << "c = " << m.str(3) << '\n';
std::cout << '\n';
}
}
}
正则表达式:https://regex101.com/r/kB2cX3/2
输出:
a = foo
b = bar
c = 15
a = baz
b = asd
c = 13
a = ddd
b = dgh
c = 66
关于C++ regex_match 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30445048/
这个问题在这里已经有了答案: Is gcc 4.8 or earlier buggy about regular expressions? (3 个答案) 关闭 6 年前。 我正在尝试对其中包含方括
#include`` #include `` #include `` using namespace std; int main () { try{ std::regex re("(http|ht
我在 site 上测试了正则表达式 [BCGRYWbcgryw]{4}\[\d\]似乎可以在以下 BBCC[0].GGRY[0].WWWW[soln] 中找到匹配项。它与 BBCC[0] 和 GGRY
我正在尝试编写递归下降解析器,并尝试在用户输入的字符串中搜索正则表达式的匹配项。我正在尝试执行以下操作以尝试理解 C++11 提供的库,但我得到了意想不到的结果。 std::string expre
我正在尝试检查 yyyy-mm-dd 格式的日期字符串的格式。我像这样调用 regex_match 函数: if (regex_match(date, regex("/\d{4}-\d{2}-\d{2
我正在尝试使用 std::regex_match()作为 std::count_if() 中的谓词与 std::vector类成员函数中的元素。但是不知道如何正确地将第二个参数(正则表达式值)绕过到函
这是我的代码: #include #include #include using namespace std; int main () { string test = "COPY" ;
我正在使用 C++ 正则表达式。无法掌握以下编程输出。 #include #include #include #include using namespace std; int main(){
我在 XCode 上使用 C++。我想使用 regex_match 匹配非字母字符,但似乎有困难: #include #include using namespace std; int main(
这个问题在这里已经有了答案: Is gcc 4.8 or earlier buggy about regular expressions? (3 个答案) 关闭 5 年前。 我有这段代码片段,是我从
这是我的部分代码 bool CSettings::bParseLine ( const char* input ) { //_asm INT 3 std::string line (
我试图弄清楚我在这个验证规则上做错了什么,因为它说了这个错误。 验证规则: $this->form_validation->set_rules('username', 'Username', 'tri
在我的代码中,我想根据封闭的 <> 括号处理 stirngs。为此,我想遍历字符串并一个一个地替换括号并根据括号内的内容做一些事情。 string msg = "This is an Example<
目标是:这个 json: {"secretWord1":"private", "something": "\"secretWord2\":\"privateToo\""} 通过 regex_match
这是我的程序: #include "stdafx.h" #include #include #include #include using namespace std; int _tmain(
我必须编写一个 C++ 正则表达式,但我无法在 regex_match 上获得正确的结果,因为我是 C++ 的新手。测试字符串为:D10A7;让我们说 unsigned_char[] stringTo
我有一些我认为应该触发的 boost Regex 代码。我是 boost 的新手,但我对 Regex 有一点了解。这是我正在使用的代码。 re = boost::basic_regex(_T("-+\
我想匹配输入字段中给定的字符串。 A sample data could be "hello" -> returns true or "\"" -> returns true or "this is
我知道: 惰性量词匹配:尽可能少(最短匹配) 还知道构造函数: basic_regex( ..., flag_type f = std::regex_constants::EC
这个问题在这里已经有了答案: Simple std::regex_search() code won't compile with Apple clang++ -std=c++14 (3 个答案)
我是一名优秀的程序员,十分优秀!