这是我的字符串
[cmd] [lPrt] [brd] [0] [{\“points \”:[\“326,191 \”,\“327,191 \”,\“328,189 \”,\“ 328,176 \“,”328,150“,”328“,124”,“316,33”,“287,79”,“199,76”,“154, 82 \“,”130,112“,”121“,195”,“121”,226“,”135,264“,”164,292“, “,”272,301“,”392,276“,”462,243“,”512,183“,”513,159“,”507,141“, \“504,138 \”,“495,135”,“485,134”,“480,134”,“470,146”,“464,158”,“ 458,174“,”458,187“,”464,217“,”484,245“,”524,302“,”556,345“,”579, 403 \“,\”586,439 \“,\”586,450 \“,\”571,459 \“,\”500,459 \“,\”373,411 \“,\”312,383 \ “,”262,262“,”260,361“,”260,361“,”DrawingType“:0,\”PenColor \“:\”Black \“,\”PenSize \“:5.0}]
我想搜索括号内的任何内容
这一定是结果:
[CMD]
[LPRT]
[BRD]
[0]
[{\“points \”:[\“326,191 \”,\“327,191 \”,\“328,189 \”,\“328,176 \”,\“328,150 \”,\ “328,124”,“316,93”,“287,79”,“199,76”,“154,82”,“130,112”,“121” ,195 \“,\”121,226 \“,\”135,264 \“,\”164,292 \“,\”184,296 \“,\”272,301 \“,\”392,276 \“,”462,243“,”512,183“,”513,159“,”507,141“,”504,138“,”495,135“ ,“485,134”,“480,134”,“470,146”,“464,158”,“458,174”,“458,187”, “464,217”,“484,245”,“524,302”,“556,345”,“579,403”,“586,439”,“586 ,450 \“,\”571,459 \“,\”500,459 \“,\”373,411 \“,\”312,383 \“,\”262,362 \“,\”260,361 \“,\”260,361 \“],\”DrawingType \“:0,\”PenColor \“:\”Black \“,\”PenSize \“:5.0}]
我尝试使用这个正则表达式参数自己做:
string param = @“[[] [\ w \ s \ d - {}'] *]”;
但它不会检测括号内的最后一个长文本..请帮助..
答案 0 :(得分:3)
您的输入有嵌套括号 - 您的语言不规则,因此您无法使用正则表达式执行所需操作。如果你想要一个解析器,你应该编写一个解析器。
如果你保证嵌套只有一层深,你可能会想出一个表达来处理它。
编辑 - 如果你的实际输入没有嵌套(与你的问题相冲突,但与你在下面的评论中所说的一致),这个正则表达式应该没问题:
\[.*?\]
正好有0或1个嵌套级别:
\[([^[\]]|\[[^[\]]*\])*\]
改编自此link的示例。
答案 1 :(得分:1)
这有助于快速测试:
(?: [[]{ .*? }] | [[] .*? ] )
我喜欢留下空白区域以便于阅读;你想要把它取出来,或者指定“IgnorePatternWhitespace”选项。
有两种匹配方式:“[{....}]”或“[...]”。它可能有点懒惰,但你可以建立一些东西。
答案 2 :(得分:0)
您的代码是一个用json填充的数组索引。
这是数组索引:
[cmd][lPrt][brd][0]
其余的都是json。现在,根据您使用的语言,您可以解码json数据并获得您正在寻找的结果。
我使用PHP并使用json_decode
解码json部分,它显示如下:
Array
(
[0] => stdClass Object
(
[points] => Array
(
[0] => 326, 191
[1] => 327, 191
[2] => 328, 189
[3] => 328, 176
[4] => 328, 150
[5] => 328, 124
[6] => 316, 93
[7] => 287, 79
[8] => 199, 76
[9] => 154, 82
[10] => 130, 112
[11] => 121, 195
[12] => 121, 226
[13] => 135, 264
[14] => 164, 292
[15] => 184, 296
[16] => 272, 301
[17] => 392, 276
[18] => 462, 243
[19] => 512, 183
[20] => 513, 159
[21] => 507, 141
[22] => 504, 138
[23] => 495, 135
[24] => 485, 134
[25] => 480, 134
[26] => 470, 146
[27] => 464, 158
[28] => 458, 174
[29] => 458, 187
[30] => 464, 217
[31] => 484, 245
[32] => 524, 302
[33] => 556, 345
[34] => 579, 403
[35] => 586, 439
[36] => 586, 450
[37] => 571, 459
[38] => 500, 459
[39] => 373, 411
[40] => 312, 383
[41] => 262, 362
[42] => 260, 361
[43] => 260, 361
)
[DrawingType] => 0
[PenColor] => Black
[PenSize] => 5
)
)