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 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 = 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: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |