c++ - 如何使用正则表达式从字符串中提取三元组值

标签 c++ regex string

我正在尝试从字符串中提取 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/

相关文章:

C++ 简单变体 boost

c# - 使用 linq 从正则表达式匹配中获取组名

c++ - 在字符串、u16string 和 u32string 之间转换

c++ - 从 const char * 或 LPCSTR 构建 std::string 时数据丢失

c++ - 删除动态数组后堆损坏

c++ - using 指令如何以及在何处注入(inject)成员名称?

c++ - 使用 C++/CLI 打开和读取 Excel 文件

java - RowFilter.regexFilter 多列

python - 如何使用pandas提取特定内容并获取android版本

java - 希望以更清洁/更有效的方式将字符串与整数相关联