473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Regular expression gone mad

Hi,
Would someone please tell me what is going on here??!! Why does the
following code work
a=r"Mem"
pat = re.compile(a)
m=pat.search(ProcMem, re.DOTALL)
m <_sre.SRE_Match object at 0xb7f7eaa0> m.group(0) 'Mem'
But this one does not!!! (Search finds nothing)
a=r"MemT"
pat = re.compile(a)
m=pat.search(ProcMem, re.DOTALL)
m

ProcMem contains:
print ProcMem

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

Feb 20 '06 #1
3 1229
fileexit:
(Search finds nothing)
a=r"MemT"
pat = re.compile(a)
m=pat.search(ProcMem, re.DOTALL)
m


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'.

--
René Pijlman
Feb 20 '06 #2
"fileexit" <ma*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi,
Would someone please tell me what is going on here??!! Why does the
following code work
a=r"Mem"
pat = re.compile(a)
m=pat.search(ProcMem, re.DOTALL)
m <_sre.SRE_Match object at 0xb7f7eaa0> m.group(0) 'Mem'
ProcMem contains:
print ProcMem

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)

Feb 20 '06 #3
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

Feb 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Des Small | last post by:
I want to use sets and regular expressions to implement some linguistic ideas. Representing sounds by symbols, and properties (coronal or velar articulation; voicedness) by sets of symbols with...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: alexstevens | last post by:
Hi All. I'm using some C# code which I translated to vb.net to remove sourcesafe information from project and solution files. It uses regular expressions, and the attempt I made at translating...
6
by: Jeff | last post by:
Hi, I'm finishing up developing a speech control jukebox (see intelligentjukebox.com) Can anyone point me to the easiest to use free library that does very basic regular expression matching...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
3
by: Lee Grissom | last post by:
I want to parse the following string into 3 parts per match. I almost got it, but my expression has a flaw in it. I tried this, but didn't quite work the way I expect on the second match b/c...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.