1

Suppose i have this string as input

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 };

And i want to extract the substring that is between { and } Of course i can scan the string until i detect { and then until i detect } and then doing substring between those indexes but there must be an easier way. how can i do it? i thought i might need to use regular expressions but i couldn't make a suitable one.

Note:the string input might contain more examples as this,i mean after the }; in the Q input stream there will be char peer0_1[]={... and so on

if you wonder where this input is coming from it's a stream content form wireshark

omer12433
  • 199
  • 1
  • 17
  • Why do you want to do this? In your C program, you will just use `peer0_0`! Your question is unclear! Why want you to parse the C initializing code above? – Basile Starynkevitch Jul 10 '14 at 15:19
  • 1
    The very least you could do is include a tag for the language you're using (or the regex dialect), and post at least a single attempt to do this yourself. – Ken White Jul 10 '14 at 15:20
  • i am using c# i forgot to mention this and i explained that my attempt is scanning for { and } and use substring between the indexes – omer12433 Jul 10 '14 at 15:22
  • I think you are looking for a regex like `[{][^{}]*[}]` [Debuggex Demo](https://www.debuggex.com/r/zMQhN_R7xtRgpmw3). – Ambrish Jul 10 '14 at 15:22
  • thank abrish that works but i need the regex to exclude the { and } from the result,i need that is between them – omer12433 Jul 10 '14 at 15:24
  • if the filke you are eating is a real C file, what about if the array is enclosed between /* and */ ? – Felice Pollano Jul 10 '14 at 15:26
  • i don't try edit C files but i was requested that the input would be in this format and i need to extract the contents of the arrays – omer12433 Jul 10 '14 at 15:27
  • @omer12433 Once you have the result, then ignore 1st and last char. – Ambrish Jul 10 '14 at 15:28
  • yeah i know i was going to do that,thank you very much and do you happen to know where i can learn regex so the next time i need them i wouldn't need to ask here? – omer12433 Jul 10 '14 at 15:29
  • @omer12433 you can practice regex here: https://www.debuggex.com/ & http://regex101.com/. Later is better as you can see what is going to happen with substitution. – Ambrish Jul 10 '14 at 15:36

1 Answers1

3

I want to extract the substring that is between { and }

Try below regex and get the matched group from index 1.

{([^}]*)

enter image description here

Explanation:

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

EDIT

The below regex will also return same result as suggested by @zx81 in comments without using capturing groups.

(?<={)[^}]*
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • 1
    thank you very much just what i needed,where can i learn regex so i could do that myself next time? – omer12433 Jul 10 '14 at 15:32
  • Visit [Learning Regular Expressions - StackOverflow](http://stackoverflow.com/questions/4736/learning-regular-expressions) – Braj Jul 10 '14 at 15:33