472,799 Members | 1,545 Online

# Regular Expression

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

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

how can I extract this number with RE or otherwise?

Michael
Jul 18 '05 #1
8 2352
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[1].split(',') [' 0.01', ' 0.02', ' 0.04']

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

On Tue, 14 Dec 2004 23:16:43 -0700, Michael McGarry
<re**********@nospam.org> wrote: 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

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

how can I extract this number with RE or otherwise?

Michael
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #2
Michael McGarry wrote:
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

".... 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 <kd***@kdart.com>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4 URL: <http://www.kdart.com/~kdart/public.key>
================================================== ==========================
Jul 18 '05 #3
Binu K S wrote:
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 average: 0.01, 0.02, 0.04'
['load average', ' 0.01, 0.02, 0.04']
[' 0.01', ' 0.02', ' 0.04']

One liner:

' 0.01'

Thank you.
Jul 18 '05 #4
Michael McGarry wrote:
Also I am trying to parse the following string to extract the number

".... 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
Jul 18 '05 #5
Michael McGarry wrote:
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>

Jul 18 '05 #6
On Tue, 14 Dec 2004 23:16:43 -0700, Michael McGarry
<re**********@nospam.org> wrote:
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

".... 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
--
Stand Fast,
tjg.
Jul 18 '05 #7
Timothy Grant <ti***********@gmail.com> writes:
On Tue, 14 Dec 2004 23:16:43 -0700, Michael McGarry
<re**********@nospam.org> wrote:

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

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().

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Jul 18 '05 #8
Michael McGarry <re**********@nospam.org> wrote:
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/boo...?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

".... 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 (',')

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)

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)

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.
Jul 18 '05 #9

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