473,320 Members | 1,978 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,320 software developers and data experts.

Concerning Regular Expressions

I've been reading a bunch of articles and tutorials on the net, but I
cannot quite get ahold of the whole regular expression process. I have
a list that contains about thirty strings, each in its own spot in the
list. What I want to do is search the list, say it's called 'lines',
for 'R0 -'.

Thanks in advanced for any and all info that I recieve.
-Tempo-

Jan 30 '06 #1
5 949
Whoops. I've got another tid-bit to tack onto this post. What kind of
value does this expression return:

re.sub(r'^R0 -', line)
Does it return a '1' if successful and a '0' if not successful?

Jan 30 '06 #2
Tempo <br*******@gmail.com> wrote:
I've been reading a bunch of articles and tutorials on the net, but I
cannot quite get ahold of the whole regular expression process. I have
a list that contains about thirty strings, each in its own spot in the
list. What I want to do is search the list, say it's called 'lines',
for 'R0 -'.


No need for regular expressions for this task as you expressed it:

for aline in lines:
if 'R0 -' in aline:
doyourworst(aline)

I've sweated substantial amount of blood and other bodyfluiids trying to
explain "the whole regular expresson process" concisely and effectively
in chapter 9 of "Python in a Nutshell" (and I'm going through the
heartache of preparing the second edition now -- let's say I'm currently
grasping for any straw to avoid the work;-) -- if I didn't do a good
enough job there, I sure can't do better here. But one thing stands: if
you don't yet understand fully the abilities of Python's strings and
other builtins, as for example in the above snippet, studying REs is
premature. WAY premature.
Alex
Jan 30 '06 #3

"Tempo" <br*******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Whoops. I've got another tid-bit to tack onto this post. What kind of
value does this expression return:

re.sub(r'^R0 -', line)
Does it return a '1' if successful and a '0' if not successful?


Start up an interactive interpreter window, try it, and see.
Such discoveries are one thing the interactive mode is designed for.


Jan 30 '06 #4
You are right that my move towards regular expressions was way
premature, but also this post may too turn out to be a little
premature. I guessed and checked myself a way to accomplish what I
needed and I will include it in this post. But first Alex (doesn't have
to be Alex) could you tell me if your snipplet and mine would be near
perfect subsitutes for one another? I believe they accomplish the same
task of looking for 'R0 -' in the list 'lines', however, as you have
guessed, I do not know my way around Python very well yet. Here is my
snipplet:

log = open('C:\log_0.txt')
lines = log.readlines()
import re
for line in lines:
R = re.search(r'^R0', line)
if R != None:
n = 1
print n
import time
time.sleep(3)
log.close()

Jan 30 '06 #5
Tempo <br*******@gmail.com> wrote:
You are right that my move towards regular expressions was way
premature, but also this post may too turn out to be a little
premature. I guessed and checked myself a way to accomplish what I
needed and I will include it in this post. But first Alex (doesn't have
to be Alex)
Uh, I think I DO have to be Alex, sorry. It's sort of, my job
description, if you will...
could you tell me if your snipplet and mine would be near
perfect subsitutes for one another? I believe they accomplish the same
task of looking for 'R0 -' in the list 'lines', however, as you have
guessed, I do not know my way around Python very well yet. Here is my
snipplet:

log = open('C:\log_0.txt')
lines = log.readlines()
import re
for line in lines:
R = re.search(r'^R0', line)
if R != None:
n = 1
print n
import time
time.sleep(3)
log.close()


Your re.search looks for R0 at the beginning of a line, and there is no
hint whatsoever of 'R0 -'. I.e., your loop here is more like:

for line in lines:
if line.startswith('R0'):
print 1

etc. I won't even guess what the stuff AFTER the loop means;-).
Alex
Jan 30 '06 #6

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

Similar topics

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: Oly | last post by:
give your opinions concerning our site,end work first year web technology www.zwin.tk in java: -navigation -stylechanger (with cookie) -pamorama -gallery
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
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...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
9
by: Thomas Ploch | last post by:
Hello fellow pythonists, I have a question concerning posting code on this list. I want to post source code of a module, which is a homework for university (yes yes, I know, please read...
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...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.