ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] pattern searching
by Shashwat Anand other posts by this author
Nov 6 2009 6:14PM messages near this date
Re: [Tutor] pattern searching | Re: [Tutor] pattern searching
@Bob: the solution seems promising, and it's fast. Thanks for the
improvement. However I would like to do a minor change to the code to
prevent it going to infinite loop.

import string

text = raw_input()
translationTable = string.maketrans(string.ascii_uppercase +
string.ascii_lowercase, 'u'*26 + 'l'*26)
translatedText = text.translate(translationTable)
start = 0
while True:
 start = translatedText.find('lul', start)
 if start > = 0:
  print text[start:start+3]
  start += 1
 else:
  break


On Sat, Nov 7, 2009 at 7:20 AM, bob gailer <bgailer@[...].com>  wrote:

>  Ajith Gopinath wrote:
> 
> > Hi,
> >
> > How to find out all the occuerence of a particular pattern like  in a long
> > text where a capital letter in between two small letters ('aBa','dAd' etc..)
> >
> 
>  The other proposals are all good. However if performance is a concern then
>  I'd use string.maketran to create a translation table, then apply it to the
>  text using translate, such that all lower case letters are translated to
>  'l', all upper case letters to 'u', then look for 'lul'.
> 
>  import string
>  translationTable = string.maketrans(string.ascii_uppercase +
>  string.ascii_lowercase, 'u'*26 + 'l'*26)
>  translatedText = text.translate(translationTable)
>  start = 0
>  while True:
>   start = translatedText.find('lul', start)
>   if start >= 0:
>    print text[start:start+3]
>   else:
>    break
> 
>  Translate and find are both very fast.
> 
>  --
>  Bob Gailer
>  Chapel Hill NC
>  919-636-4239
> 
>  _______________________________________________
>  Tutor maillist  -  Tutor@[...].org
>  To unsubscribe or change subscription options:
>  http://mail.python.org/mailman/listinfo/tutor
> 
Thread:
Ajith Gopinath
Bob Gailer
Shashwat Anand
Bob Gailer
Ajith Gopinath
Lie Ryan
Bob Gailer
Modulok
Shashwat Anand

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved