Regular Expression

M

Michael McGarry

Hi,

I am horrible with Regular Expressions, can anyone recommend a book on it?

Also I am trying to parse the following string to extract the number
after load average.

".... load average: 0.04, 0.02, 0.01"

how can I extract this number with RE or otherwise?

Michael
 
B

Binu K S

You can do this without regular expressions if you like
uptime='12:12:05 up 21 days, 16:31, 10 users, load average: 0.01, 0.02, 0.04'
load = uptime[uptime.find('load average:'):]
load 'load average: 0.01, 0.02, 0.04'
load = load.split(':')
load ['load average', ' 0.01, 0.02, 0.04']
load[1].split(',')
[' 0.01', ' 0.02', ' 0.04']

One liner:
uptime[uptime.find('load average:'):].split(':')[1].split(',')[0]
' 0.01'
 
K

Keith Dart

Michael said:
Hi,

I am horrible with Regular Expressions, can anyone recommend a book on it?

Also I am trying to parse the following string to extract the number
after load average.

".... load average: 0.04, 0.02, 0.01"

how can I extract this number with RE or otherwise?

This particular example might be parsed more quickly and easily just by
chopping it up:

s = ".... load average: 0.04, 0.02, 0.01"
[left, right] = s.split(":")
[av1, av2, av3] = map(float, map(str.strip, right.split(",")))



--
\/ \/
(O O)
-- --------------------oOOo~(_)~oOOo----------------------------------------
Keith Dart <[email protected]>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4 URL: <http://www.kdart.com/~kdart/public.key>
============================================================================
 
M

Michael McGarry

S

Steven Bethard

Michael said:
Also I am trying to parse the following string to extract the number
after load average.

".... load average: 0.04, 0.02, 0.01"

In Python 2.4:
>>> uptime='12:12:05 up 21 days, 16:31, 10 users, load average: 0.01, 0.02, 0.04'
>>> _, avg_str = uptime.rsplit(':', 1)
>>> avg_str ' 0.01, 0.02, 0.04'
>>> avgs = [float(s) for s in avg_str.split(',')]
>>> avgs
[0.01, 0.02, 0.040000000000000001]

I took advantage of the new str.rsplit function which splits from the
right side instead of the left.

Steve
 
F

Fredrik Lundh

Michael said:
I am horrible with Regular Expressions, can anyone recommend a book on it?

Also I am trying to parse the following string to extract the number after load average.

".... load average: 0.04, 0.02, 0.01"

how can I extract this number with RE or otherwise?

others have shown you that you don't really need RE:s in this case; just
split away until you have the right parts.

here's a RE solution:

text = ".... load average: 0.04, 0.02, 0.01"
print re.findall("\d[.\d]*", text)

"\d" matches a digit, "[.\d]" matches either a digit or a dot, and "*" says
that the immediately preceeding RE part can be repeated zero or more
times. in other words, we're looking for a digit followed by zero or more
digits or dots.

to get floating point numbers, map the result through "float".

note that you can create pickier patterns (that ignores things like "1...."
and "1.1.1.1", for example) but that's overkill in this case.

</F>
 
T

Timothy Grant

Hi,

I am horrible with Regular Expressions, can anyone recommend a book on it?

Also I am trying to parse the following string to extract the number
after load average.

".... load average: 0.04, 0.02, 0.01"

how can I extract this number with RE or otherwise?

Lot's of good solutions for the problem.

However, you might want to check out Jeffrey Friedl's book Mastering
Regular Expressions, published by O'Reilly.
 
M

Michael Fuhr

Timothy Grant said:
Lot's of good solutions for the problem.

In the special case where you want the current load average numbers
for the box running the program and you have Python 2.3 or later,
you could use os.getloadavg().
 
R

Roy Smith

Michael McGarry said:
I am horrible with Regular Expressions, can anyone recommend a book on it?

I just did a search on the Barnes & Noble site for "regular expression"
and came up with a bunch of books. The following looks reasonable:

http://search.barnesandnoble.com/booksearch/isbnInquiry.asp?userid=Ft1ixM
aAY9&isbn=0596002890&itm=1

but I find the blub kind of funny. It says:
Despite their wide availability, flexibility, and unparalleled
power, regular expressions are frequently underutilized. Regular
expressions allow you to code complex and subtle text processing
that you never imagined could be automated. Regular expressions can
save you time and aggravation. They can be used to craft elegant
solutions to a wide range of problems.

On the other hand, they are frequently overused. Back in the days of
steam-powered computers, utilities like ed and grep ruled the landscape.
The only text parsing tool we had was regular expressions, so that's
what we used for everything, and we got good at them out of necessity.
Every once in a while we came upon a problem regexs couldn't solve, so
our toolbuilders built us ever more powerful regex libraries (Henry
Spencer's version probably being the best) allowing us to write ever
more complex regular expressions.

These days, regex is still a powerful tool, and undeniably the best tool
in certain situations. But there are simplier and easier tools for many
every-day jobs. I think this example is one of those.
Also I am trying to parse the following string to extract the number
after load average.

".... load average: 0.04, 0.02, 0.01"

I would just use the split method of strings:
s = ".... load average: 0.04, 0.02, 0.01"
words = s.split()
load = words[3].strip (',')
print load
'0.04'

You could do this with regex if you wanted to. One way would be to use
regex in a simple way to deal with the fact that the "words" are
delimited by a combination of spaces and commas:
import re
words = re.split ('[, ]*', s)
load = words[3]
print load
'0.04'

which may or may not be any easier to understand. Another way would be
to use a more complex regex to match exactly the piece you want in one
step:
load = re.findall (r': ([^,]*)', s)
print load

This does the whole job in a single line, but depending on how familier
with regex's you are, it may or may not be easier to understand. Note,
that there is a fundamental difference between what this is doing and
what was being done above. In the last example, the location of the
load average string is determined by looking after the ':' for something
that fit a pattern. In the earlier two examples, it was found by
counting words from the beginning of the string. If the stuff that came
before the ':' might have a variable number of words in it, the full
regex version might be more correct.
 

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

Forum statistics

Threads
474,214
Messages
2,571,112
Members
47,704
Latest member
DavidSuita

Latest Threads

Top