C++ 未显示正则表达式子匹配项
C++ Regex Sub Matches not appearing
我正在制作一个正则表达式来修改函数中的第二个和第三个参数。我正在使用大多数 linux 发行版附带的 regex.h 库。我似乎无法让子匹配出现在下面的代码中。
法典:
string s ("tweenArray.push(addNewPoint(posTween, 1 , 2, .3, scale, brushWidth));");
regex e ("(tweenArray.push\(addNewPoint\(posTween,)s*(.+?),s*(.+?),"); // matches words beginning by "sub"
smatch m;
regex_match ( s, m, e );
cout << "match " << 0 << " (" << m[0] << ")" << endl;
cout << "match " << 1 << " (" << m[1] << ")" << endl;
cout << "match " << 2 << " (" << m[2] << ")" << endl;
cout << "match " << 3 << " (" << m[3] << ")" << endl;
cout << regex_replace (s,e,"$1 0, 0");
输出:
match 0 ()
match 1 ()
match 2 ()
match 3 ()
tweenArray.push(addNewPoint(posTween, 0 , 0, .3 scale, brushWidth));
替换工作得很好,这告诉我正则表达式是正确的。但是,不会显示子匹配项。为什么子比赛不显示?
我认为regex_match需要整个字符串匹配。
Important
Note that the result is true only if the expression matches the whole of
the input sequence. If you want to search for an expression somewhere
within the sequence then use regex_search. If you want to match a prefix of
the character string then use regex_search with the flag match_continuous set.
所以,也许这会起作用
".*?tweenArray.push\(addNewPoint\(posTween,\s*(.+?),\s*(.+?),.*"
.*?
tweenArray . push(addNewPoint(posTween,
s*
( .+? ) # (1)
, s*
( .+? ) # (2)
,
.*
输出:
** Grp 0 - ( pos 0 , len 69 )
tweenArray.push(addNewPoint(posTween, 1 , 2, .3, scale, brushWidth));
** Grp 1 - ( pos 38 , len 2 )
1
** Grp 2 - ( pos 42 , len 1 )
2
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++正则表达式无限循环
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 定义有趣的宏和正则表达式在Z3 C++绑定
- 带有多个字符分隔符的正则表达式
- C++ 使用增强正则表达式库时断言崩溃
- 有人可以帮助我处理正则表达式吗?
- 使用正则表达式获取大括号块的列表
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 组合正则表达式部分使用 | 不起作用的 C++
- 为什么C++正则表达式这么慢?
- 如何使HTML5电子邮件验证正则表达式在C++中工作?
- 在C++中实现正则表达式
- C++正则表达式替换整个单词
- 用C++编写正则表达式的正确方法是什么?
- 如何从Stroustrup的C++书中解释这个正则表达式?
- 为什么这个正则表达式C++在括号表达式中抛出无效范围异常?
- C++:匹配正则表达式,什么是匹配?
- 我如何写一个Qt正则表达式来突出显示字符串开始和结束与非单词字符