[Tutor] Question on Regular Expression Help

Henry Steigerwaldt hsteiger@comcast.net
Sat Feb 8 01:04:24 2003


This is a multi-part message in MIME format.

--Boundary_(ID_q5KDbCler5Dw4K9hjxsNsQ)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7BIT

To All:

I have spent some time without success, trying to read a line
of data containing four numbers and storing each number into
a variable. 

The line that is read is the following, which has been shortened
some to remove extra spaces for this example.

MX/MN          91          72          90          70         

These numbers represent temperatures, and can vary, such as
-11, -1, 0, 9 23, 75, 102, etc. So in order to use a regular expression
to search for the numbers, I have to check for a dash (-) with up to 
2 digits after the dash, and whether their could be up to 3 digits in
each number. 

The problem is that my code cannot extract these numbers to
store into the variables t1, t2, t3, and t4. The code does
find something because the piece of code below, namely

   if temp == None:
        print "temp is MISSING"

        return result

is not accessed. 

I use a list below because there are a number of files to open

and store each file into a variable for later number extraction.

My code (with comments is as follows).

_________________________

fwc[j] = f_object.read()   
f_object.seek(0)   # reset pointer to beginning of file

string = f_object.readline()    # read and store one line into variable

regexp = re.compile(r"(?P<temp1>[-]*[1]*[0-9]*)"

      r"(?P<temp2>[-]*[1]*[0-9]*)"

      r"(?P<temp3>[-]*[1]*[0-9]*)"

      r"(?P<temp4>[-]*[1]*[0-9]*)")

temp = regexp.search(string)     #  store entire pattern found into "temp"

if temp == None:

     print "temp is MISSING"

     return result

t1 = temp.group('temp1')     # store each number found into a variable 

t2 = temp.group('temp2')

t3 = temp.group('temp3')

t4 = temp.group('temp4')


__________________________

I have also tried the follow code for the regular expression

without success. 

regexp = re.compile(r"(?P<temp1>[ ][-]?[1]*[0-9][0-9])"

     r"(?P<temp2>[ ][-]?[1]*[0-9][0-9])"

     r"(?P<temp3>[ ][-]?[1]*[0-9][0-9])"

     r"(?P<temp4>[ ][-]?[1]*[0-9][0-9])")




I hope the problem is something simple in the regular

expressions I have tried. 



Thanks for any help provided. Please also send a response

to my email address below.

Henry Steigerwaldt

Hermitage, TN

Email:  hsteiger@comcast.net



--Boundary_(ID_q5KDbCler5Dw4K9hjxsNsQ)
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7BIT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2800.1126" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>To All:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I have spent some time without success, trying to 
read a line</FONT></DIV>
<DIV><FONT face=Arial size=2>of data containing four numbers and storing each 
number into</FONT></DIV>
<DIV><FONT face=Arial size=2>a variable. </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>The line&nbsp;that is read&nbsp;is the following, 
</FONT><FONT face=Arial size=2>which has been&nbsp;shortened</FONT></DIV>
<DIV><FONT face=Arial size=2>some to remove&nbsp;extra spaces for this 
example.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial 
size=2>MX/MN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;91&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
72&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
70&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>These numbers represent temperatures, and can vary, 
such as</FONT></DIV>
<DIV><FONT face=Arial size=2>-11, -1, 0, 9 23, 75, 102, etc. So in order to use 
a regular expression</FONT></DIV>
<DIV><FONT face=Arial size=2>to search for the numbers,&nbsp;</FONT><FONT 
face=Arial size=2>I&nbsp;have to check for a </FONT><FONT face=Arial size=2>dash 
(-) with up to </FONT></DIV>
<DIV><FONT face=Arial size=2>2 digits after the dash, and whether </FONT><FONT 
face=Arial size=2>their could be up to 3 digits in</FONT></DIV>
<DIV><FONT face=Arial size=2>each </FONT><FONT face=Arial size=2>number. 
</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>The&nbsp;problem is that my code cannot extract 
these numbers to</FONT></DIV>
<DIV><FONT face=Arial size=2>store into the&nbsp;variables t1, t2, t3, and t4. 
The code does</FONT></DIV>
<DIV><FONT face=Arial size=2>find something because the piece of code below, 
namely</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>&nbsp;&nbsp; if temp == None:</DIV>
<DIV>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; print "temp is MISSING"</P><FONT 
size=2>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return result</P>
<P>is not accessed. </P>
<P>I use a list below because there are a number of files to open</P>
<P>and store each file into a variable for later number 
extraction.</P></FONT></FONT>
<P><FONT face=Arial size=2>My code (with comments is as follows).</FONT></P>
<P><FONT face=Arial size=2>_________________________</FONT></P><FONT face=Arial 
size=2></DIV>
<DIV></FONT><FONT face=Arial size=2>fwc[j] = f_object.read()&nbsp;</FONT><FONT 
face=Arial size=2>&nbsp;&nbsp;</FONT></DIV>
<DIV>
<P><FONT face=Arial size=2><FONT size=2>f_object.seek(0)&nbsp;&nbsp; 
#&nbsp;reset pointer to beginning of file</P>
<P></FONT></FONT><FONT face=Arial size=2>string = 
f_object.readline()&nbsp;&nbsp;&nbsp; # read and store one line into 
variable</FONT></P>
<P><FONT face=Arial size=2>regexp = 
re.compile(r"(?P&lt;temp1&gt;[-]*[1]*[0-9]*)"</FONT></P>
<P><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
r"(?P&lt;temp2&gt;[-]*[1]*[0-9]*)"</FONT></P>
<P><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
r"(?P&lt;temp3&gt;[-]*[1]*[0-9]*)"</FONT></P>
<P><FONT face=Arial size=2>&nbsp; &nbsp;&nbsp;&nbsp; 
r"(?P&lt;temp4&gt;[-]*[1]*[0-9]*)")</FONT></P><FONT face=Arial size=2><FONT 
size=2>
<P>temp = regexp.search(string)&nbsp;&nbsp;&nbsp;&nbsp; #&nbsp; store entire 
pattern found into "temp"</P></FONT></FONT><FONT face=Arial size=2><FONT size=2>
<P>if temp == None:</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; print "temp is MISSING"</P><FONT size=2>
<P>&nbsp;&nbsp;&nbsp;&nbsp; return result</P><FONT size=2>
<P>t1 = temp.group('temp1')&nbsp;&nbsp;&nbsp;&nbsp; # store each number found 
into a variable </P>
<P>t2 = temp.group('temp2')</P>
<P>t3 = temp.group('temp3')</P>
<P>t4 = temp.group('temp4')</P>
<P></P></FONT>
<P>__________________________</P>
<P>I have also tried the follow code for the&nbsp;regular expression</P>
<P>without success. </P>
<P><FONT size=2>regexp = re.compile(r"(?P&lt;temp1&gt;[ 
][-]?[1]*[0-9][0-9])"</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; r"(?P&lt;temp2&gt;[ ][-]?[1]*[0-9][0-9])"</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; r"(?P&lt;temp3&gt;[ ][-]?[1]*[0-9][0-9])"</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp; r"(?P&lt;temp4&gt;[ ][-]?[1]*[0-9][0-9])")</P>
<P></P></FONT>
<P>&nbsp;</P>
<P>I&nbsp;hope the problem is something simple in the regular</P>
<P>expressions I have tried. </P>
<P>&nbsp;</P>
<P>Thanks for any help provided. Please also send&nbsp;a response</P>
<P>to my email address below.</P>
<P>Henry Steigerwaldt</P>
<P>Hermitage, TN</P>
<P>Email:&nbsp; <A 
href="mailto:hsteiger@comcast.net">hsteiger@comcast.net</A></P>
<P>&nbsp;</P></FONT></FONT></FONT></DIV></BODY></HTML>

--Boundary_(ID_q5KDbCler5Dw4K9hjxsNsQ)--