Skip to content

Instantly share code, notes, and snippets.

@adimiti
Created May 7, 2021 10:00
Show Gist options
  • Select an option

  • Save adimiti/d35a23583bc958471571beb0846a5c9f to your computer and use it in GitHub Desktop.

Select an option

Save adimiti/d35a23583bc958471571beb0846a5c9f to your computer and use it in GitHub Desktop.
parse 2d C array
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