import re
from unix.sizes import kb2b

_MEMINFO = '/proc/meminfo'

_PARAM_RE = re.compile('(([a-zA-Z0-9_]*)(\((\w*)\))?):\s+(\d*)(.*)?')

class Memory(object):
    def __init__(self, host):
        self._host = host

        with self._host.open(_MEMINFO) as fhandler:
            for line in fhandler.read().splitlines():
                regex = _PARAM_RE.search(line.decode()).groups()
                param = regex[1].lower()
                if regex[3]:
                    param = '%s_%s' % (param, regex[3])
                if param.startswith('mem'):
                    param = param[3:]

                value = kb2b(regex[4], si=True) if regex[5] else regex[4]
                setattr(self, param, value)

@@ -87,11 +87,21 @@
 ile(
-'((
+r'%5E(?P%3Cparam%3E
 %5Ba-z
@@ -118,31 +118,89 @@
 (%5C((
-%5Cw*)%5C))?):%5Cs+(%5Cd*)(.*)?
+?P%3Canon%3E%5Cw*)%5C))?:'%0A                        '%5Cs+(?P%3Cvalue%3E%5Cd*)(%5Cs+(?P%3Cunit%3E%5Cw*))?$
 ')%0A%0A
@@ -445,17 +445,20 @@
 )).group
-s
+dict
 ()%0A     
@@ -486,9 +486,15 @@
 gex%5B
-1
+'param'
 %5D.lo
@@ -518,24 +518,39 @@
      if 
-regex%5B3%5D
+param.startswith('mem')
 :%0A      
@@ -575,117 +575,112 @@
 m = 
-'%25s_%25s' %25 (param, regex%5B3%5D)%0A                if param.startswith('mem'):%0A                    param = param%5B3:%5D
+param%5B3:%5D%0A                if regex%5B'anon'%5D:%0A                    param = '%25s_%25s' %25 (param, regex%5B'anon'%5D)
 %0A%0A  
@@ -712,17 +712,23 @@
 b(regex%5B
-4
+'value'
 %5D, si=Tr
@@ -744,9 +744,14 @@
 gex%5B
-5
+'unit'
 %5D el
@@ -763,9 +763,15 @@
 gex%5B
-4
+'value'
 %5D%0A  

Correct a bug with the regular expression raising an error on some systems at the import.