Regular expression gone mad

F

fileexit

Hi,
Would someone please tell me what is going on here??!! Why does the
following code work
'Mem'


But this one does not!!! (Search finds nothing)


ProcMem contains:
MemTotal: 8247952 kB
MemFree: 5980920 kB
Buffers: 417044 kB
Cached: 703036 kB
SwapCached: 0 kB
Active: 1440136 kB
Inactive: 370668 kB
HighTotal: 7405512 kB
HighFree: 5977600 kB
LowTotal: 842440 kB
LowFree: 3320 kB
SwapTotal: 8339440 kB
SwapFree: 8339296 kB
Dirty: 96 kB
Writeback: 0 kB
Mapped: 786672 kB
Slab: 359208 kB
Committed_AS: 2453912 kB
PageTables: 24696 kB
VmallocTotal: 106488 kB
VmallocUsed: 8700 kB
VmallocChunk: 96708 kB
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 2048 kB
 
R

Rene Pijlman

fileexit:
(Search finds nothing)

From the docs:

"Compiled regular expression objects support the following methods and
attributes:
match( string[, pos[, endpos]])"

Your re.DOTALL is seen as start position. That's why it's probably
matching 'MemFree' instead of 'MemTotal'.
 
P

Paul McGuire

fileexit said:
Hi,
Would someone please tell me what is going on here??!! Why does the
following code work

'Mem'


ProcMem contains:

MemTotal: 8247952 kB
MemFree: 5980920 kB
Buffers: 417044 kB
Cached: 703036 kB
SwapCached: 0 kB
Active: 1440136 kB
Inactive: 370668 kB
HighTotal: 7405512 kB
HighFree: 5977600 kB
LowTotal: 842440 kB
LowFree: 3320 kB
SwapTotal: 8339440 kB
SwapFree: 8339296 kB
Dirty: 96 kB
Writeback: 0 kB
Mapped: 786672 kB
Slab: 359208 kB
Committed_AS: 2453912 kB
PageTables: 24696 kB
VmallocTotal: 106488 kB
VmallocUsed: 8700 kB
VmallocChunk: 96708 kB
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 2048 kB

Are you going to create re's for every line of that data? Here's a
pyparsing version that generates the grammar for you (or you can modify this
example to generate re's if you prefer).

This is an unusual form for pyparsing. Typically, people construct an And in
their grammar by connecting expressions together with '+' operators (as in
the procMemEntry method of the example). But here, we are generating the
list of entries, and then directly creating the And with the resulting list
of expressions.

Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul

procMemData = """
MemTotal: 8247952 kB
MemFree: 5980920 kB
Buffers: 417044 kB
Cached: 703036 kB
SwapCached: 0 kB
Active: 1440136 kB
Inactive: 370668 kB
HighTotal: 7405512 kB
HighFree: 5977600 kB
LowTotal: 842440 kB
LowFree: 3320 kB
SwapTotal: 8339440 kB
SwapFree: 8339296 kB
Dirty: 96 kB
Writeback: 0 kB
Mapped: 786672 kB
Slab: 359208 kB
Committed_AS: 2453912 kB
PageTables: 24696 kB
VmallocTotal: 106488 kB
VmallocUsed: 8700 kB
VmallocChunk: 96708 kB
HugePages_Total: 0
HugePages_Free: 0
Hugepagesize: 2048 kB
"""
from pyparsing import Word,nums,Literal,Suppress,Group,And,Dict

# define an integer, and an integer kB value
integer = Word(nums).setParseAction(lambda s,l,t:int(t[0]))
numKb = integer + Suppress("kB")

# convenience method for extracting procMem entries
def procMemEntry(name, valExpr):
return Group(Literal(name) + Suppress(":") + valExpr).setName(name)

# extract names from sample procMem data, and create list of matching value
expressions
names = [l.split(':')[0] for l in procMemData.split('\n') if l]
exprs = [n.startswith("HugePages_") and integer or numKb for n in names]

# generate grammar using names and exprs lists
procMemGrammar = Dict(And([ procMemEntry(nam,expr) for nam,expr in
zip(names,exprs) ]))

# check grammar by parsing input string
pmData = procMemGrammar.parseString(procMemData)

# access pmData as a dict
for k in pmData.keys():
print k,pmData[k]

# or create a standard Python dict from pmData
print dict(pmData)
 
P

plahey

This may not be enough for what you really want to do, but, given what
you have shown above, regex is the wrong tool for the job.

Use the string method startswith:

for line in ProcMem.split('\n'):
if line.startswith( 'Mem' ):
# do stuff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top