[Tutor] Help with re in Python 3

Albert-Jan Roskam fomcl at yahoo.com
Fri Nov 4 20:59:42 CET 2011


It seems that you are not opening the file properly. You could do
f = file('///Users/joebatt/Desktop/python3.txt','r')
or:
withfile('///Users/joebatt/Desktop/python3.txt','r') as f:
  for line in f:
    m = re.search("[A-Z]{3}[a-z][A-Z]{3}", line)
    if m:
      print("Pattern found")
      print(m.group(0))
 
Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


>________________________________
>From: Joe Batt <joebatt at hotmail.co.uk>
>To: tutor at python.org
>Sent: Friday, November 4, 2011 8:42 PM
>Subject: [Tutor] Help with re in Python 3
>
>
> 
>Hi all,
>Still trying with Python and programming in general…. 
>
>
>I am trying to get a grip with re. I am writing a program to open a text file and scan it for exactly 3 uppercase letters in a row followed by a lowercase followed by exactly 3 uppercase letters. ( i.e.  oooXXXoXXXooo )
>If possible could you explain why I am getting "EOL while scanning string literal" when I try running the following program in Python 3.
>
>
>My program:
>
>
>import re
>regexp=re.compile(r"[a-z]"r"[A-Z]"r"[A-Z]"r"[A-Z]"r"[a-z]"r"[A-Z]"r"[A-Z]"r"[A-Z]"r"[a-z])
>                  
>file=('///Users/joebatt/Desktop/python3.txt','r')
>for line in file.readlines():
>    if regexp.search(line):
>        print("Found value 3 caps followed by lower case followed by 3 caps")
>file.close()
>
>
>If possible could you explain why I am getting "EOL while scanning string literal" when I try running my program in Python 3.
>
>
>Thanks for your help
>
>
>Joe
>
>
>
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111104/f8b8abd8/attachment.html>


More information about the Tutor mailing list