我正在尝试从字符串中提取 n 个三元组(Si、Pi、Vi)。
字符串至少包含一个这样的三元组。 Pi 和 Vi 不是强制性的。
SomeTextxyz@S1((property(P1)val(V1))@S2((property(P2)val(V2))@S3
|----------1-------------|----------2-------------|-- n
期望的输出是:
Si,Pi,Vi.
因此对于字符串中的 n 次出现,输出应如下所示:
[S1,P1,V1] [S2,P2,V2] ... [Sn-1,Pn-1,Vn-1] (without the brackets)
示例
输入的字符串可能是这样的:
MyCarGarage@Mustang((property(PS)val(500))@Porsche((property(PS)val(425)).
处理后的输出应该是:
Mustang,PS,500 Porsche,PS,425
有没有一种使用正则表达式提取这些三元组的有效方法
(例如,使用 C++ 和 std::regex
)它会是什么样子?
最佳答案
@(.*?)\(\(property\((.*?)\)val\((.*?)\)\)
应该可以解决问题。
例子在 http://regex101.com/r/bD1rY2
@ # Matches the @ symbol
(.*?) # Captures everything until it encounters the next part (ungreedy wildcard)
\(\(property\( # Matches the string "((property(" the backslashes escape the parenthesis
(.*?) # Same as the one above
\)val\( # Matches the string ")val("
(.*?) # Same as the one above
\)\) # Matches the string "))"
我不知道你应该如何在 C++ 中实现它,但这是简单的部分 :)
关于c++ - 如何使用正则表达式从字符串中提取三元组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16799511/