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 8 2407
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'
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 after load average.
".... 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
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 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 <kd***@kdart.com>
vcard: <http://www.kdart.com/~kdart/kdart.vcf>
public key: ID: F3D288E4 URL: <http://www.kdart.com/~kdart/public.key>
================================================== ==========================
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 = 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'
Thank you.
Michael McGarry wrote: 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
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>
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 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.
--
Stand Fast,
tjg.
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/
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 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
by: Neri |
last post by:
Some document processing program I write has to deal with documents
that have headers and footers that are unnecessary for the main
processing part. Therefore, I'm using a regular expression to go...
|
by: Dimitris Georgakopuolos |
last post by:
Hello,
I have a text file that I load up to a string. The text includes
certain expression like {firstName} or {userName} that I want to match
and then replace with a new expression. However,...
|
by: James D. Marshall |
last post by:
The issue at hand, I believe is my comprehension of using regular
expression, specially to assist in replacing the expression with other text.
using regular expression (\s*) my understanding is...
|
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...
|
by: Pete Davis |
last post by:
I'm using regular expressions to extract some data and some links from some
web pages. I download the page and then I want to get a list of certain
links.
For building regular expressions, I use...
|
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...
|
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...
|
by: NvrBst |
last post by:
I want to use the .replace() method with the regular expression /^ %VAR
% =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)"
regular expression with "":...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |