0

我有以下测试字符串。

#5=BUILDING('xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$);
#6=BUILDING('xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$);
#7=BUILDING('xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$);

我需要提取:

  • “#integer”(始终从字符串的开头开始)从上面的字符串中提取并将其存储在变量中。
  • 上面测试字符串中“(”和“)”之间的字符串。

有人可以建议我如何使用正则表达式在 C++ 中实现这一点。

我尝试按照简单的示例进行操作(这是一个一次处理一行的循环):

std::regex e ("\#[:d:]+");
if (std::regex_match(sLine,e)){
   //store it and process it
}

输出应该是:

#5

and

'xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$ ?? (not sure)
4

1 回答 1

1

Description

This expression will:

  • capture the initial # and integer
  • capture the value between the parentheses

^(\#\d+).*?\(([^)]*)\)

enter image description here

Example

Live Demo

Sample Text

#5=BUILDING('xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$);
#6=BUILDING('xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$);
#7=BUILDING('xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$);

Capture Groups

Group 0 gets the entire matched string
Group 1 gets the # and integer
Group 2 gets the value between the parentheses

[0][0] = #5=BUILDING('xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$)
[0][1] = #5
[0][2] = 'xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$

[1][0] = #6=BUILDING('xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$)
[1][1] = #6
[1][2] = 'xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$

[2][0] = #7=BUILDING('xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$)
[2][1] = #7
[2][2] = 'xxxcdccx',#5,$,$,$,#21,$,$,.ELEMENT.,$,$,$
于 2013-08-02T12:17:12.000 回答