[Tutor] How to get user input as regex

Pierre Barbier de Reuille pierre.barbier at cirad.fr
Sat Apr 2 17:22:01 CEST 2005


You should do something much simpler ^_^ :

import re
s = raw_input("regex : ")
digs = re.compile(s)
[...]

That should work :)

pierre

Dick Moores a écrit :
> I'm just beginning to learn regex, and am wondering how to get user 
> input that can be used as a regex. Of course my attempt below doesn't 
> work, but how should I modify it?
> 
> I'm practicing with phone.txt, which is 513 lines, each line having a 
> name and phone number, and some other details like an address and times.
> 
> Sorry for the dumb question.
> 
> import re
> s = raw_input("regex: ")
> digs = re.compile(r"s")
> for line in open("data//phone.txt"):
>     if digs.search(line):
>         print line
> 
> Thanks,
> 
> Dick Moores
> rdm at rcblue.com
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

-- 
Pierre Barbier de Reuille

INRA - UMR Cirad/Inra/Cnrs/Univ.MontpellierII AMAP
Botanique et Bio-informatique de l'Architecture des Plantes
TA40/PSII, Boulevard de la Lironde
34398 MONTPELLIER CEDEX 5, France

tel   : (33) 4 67 61 65 77    fax   : (33) 4 67 61 56 68


More information about the Tutor mailing list