Concerning Regular Expressions

T

Tempo

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-
 
T

Tempo

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?
 
A

Alex Martelli

Tempo said:
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
 
T

Terry Reedy

Tempo said:
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.
 
T

Tempo

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

Alex Martelli

Tempo said:
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
 

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,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top