Created
May 7, 2021 10:00
-
-
Save adimiti/d35a23583bc958471571beb0846a5c9f to your computer and use it in GitHub Desktop.
parse 2d C array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data = ''' | |
| static const unsigned char array[3][5] = | |
| { | |
| { 0x00, 0x01, 0x02, 0x03, 0x04 }, // " " | |
| { 0x10, 0x11, 0x12, 0x13, 0x14 }, // X | |
| { 0x20, 0x21, 0x22, 0x23, 0x24 }, // ~ | |
| }; | |
| ''' | |
| import re | |
| # definition | |
| x = re.search(r'\[(\d+)\]\s*\[(\d+)\]\s', data) | |
| m, n = (int(i) for i in x.groups()) | |
| print('rows =', m, 'columns =', n) | |
| #line | |
| h = r'(0[xX][0-9a-fA-F]+)' | |
| hc = h + r'\s*,\s*' | |
| e = r'\s*{\s*' + hc * (n-1) + h + r',?\s*}\s*,\s*//\s*(.*)' | |
| x = re.search(r'\s*{' + e * m + r'\s*}\s*;', data[data.index('{'):], re.MULTILINE) | |
| print('x =', x) | |
| print(x and x.groups()) | |
| # print 'start =', data[data.index('{'):] | |
| print 'e =', e | |
| y = x.groups() | |
| print y[n::n+1] | |
| print [b for a,b in enumerate(y) if n != (a % (n+1))] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment