获取{和}之间的子字符串

时间:2014-07-10 15:15:55

标签: c# regex string

假设我有这个字符串作为输入

  

char peer0_0 [] = {0x17,0x03,0x03,0x00,0xa2,0x00,0x00,0x00,   0x00,0x00,0x00,0x01,0xc3,0xb3,0xee,0x9a,0x37,0xb6,0xbf,   0x8f,0x89,0x58,0xe4,0x8d,0x8a,0x0b,0xe8,0x98,0xba,0x49,   0x0f,0x45,0x7c,0x93,0x65,0x7b,0x15,0x78,0xde,0xe2,0x9b,   0xd2,0x9b,0x26,0x27,0x4f,0x3d,0x1f,0xf4,0x4e,0x0e,0xcf,   0xa8,0x60,0xed,0x45,0x2f,0x63,0xeb,0x4e,0xcc,0x55​​,0x6f,   0x2f,0x57,0x0e,0x7b,0x7f,0xd1,0xcb,0xc9,0x87,0x06,0x9f,   0x81,0x8e,0x37,0x80,0xf2,0x9f,0xa0,0xa4,0x06,0x75,0x06,   0x45,0x4c,0x21,0x51,0x1a,0x6a,0x4b,0x26,0x9c,0xdf,0xee,   0xbc,0x03,0xee,0x31,0xa7,0x2a,0x46,0xea,0x91,0x91,0x6b,   0x6f,0xc1,0xa6,0xf7,0x3e,0x16,0x98,0x63,0x67,0x86,0x2f,   0xfb,0x14,0x8e,0xd6,0xcd,0x14,0x2c,0xf7,0xbf,0x91,0x18,   0x89,0xaf,0xad,0xdf,0x09,0x2e,0xc0,0x20,0x1c,0x27,0xf9,   0xba,0xf4,0xc7,0xf2,0x7e,0x0d,0x1d,0x64,0x4b,0x85,0x7e,   0xd7,0x0f,0xeb,0x24,0x2f,0x3a,0x61,0x3d,0x5e,0x65,0x75,   0x81,0x34,0xf6,0x00,0x2c};

我想提取{}之间的子字符串 当然我可以扫描字符串,直到我检测到{,然后直到我检测到},然后在这些索引之间进行子串,但必须有一个更简单的方法。 我该怎么做?我以为我可能需要使用正则表达式但我无法制作合适的表达式。

注意:字符串输入可能包含更多示例,我的意思是在Q输入流中的};之后会有char peer0_1[]={...等等

如果你想知道这个输入来自哪里,那就是一个流内容形式wireshark

1 个答案:

答案 0 :(得分:3)

  

我想提取{和}

之间的子字符串

尝试以下正则表达式并从索引1获取匹配的组。

{([^}]*)

enter image description here

说明:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  {                        '{'
  (                        group and capture to \1:
    [^}]*                    any character except: '}' (0 or more
                             times (matching the most amount
                             possible))
  )                        end of \1

修改

以下正则表达式也会在不使用捕获组的情况下在注释中返回@ zx81建议的相同结果。

(?<={)[^}]*