[Tutor] putting together commands

Sean 'Shaleh' Perry shalehperry@attbi.com
Wed, 16 Oct 2002 10:42:36 -0700


On Wednesday 16 October 2002 10:00, Terje Johan Abrahamsen wrote:
> I am using the re module to find some text in a file. But, I do want th=
e
> program to find different things each time. As an example I have done t=
his:
>
> rrr =3D 'tore er en hund'
>
> I want to find whether the text has re after to.
>
> m =3D re.search('(?<=3Dto)nd', rrr)
>
> And the program finds it. But, if I want to find 50 things, and want to
> find 50 different things each time, it will be very timeconsuming to
> rewrite each one. So I write:
>
> p =3D 're.search(\'(?<=3D'
> q =3D 'hu'
> r =3D ')nd\', rrr)'
> a =3D p+q+r
>
> And I change the q 50 times, or uses it as the input in def find(q)
>

def textSearch(q, input):
  return re.search(r'(?<=3D%s)nd' % q, input) # use a string formatter (-=
:

textSearch('hu', rrr)
textSearch('to', rrr)