[Tutor] regex question

Steven D'Aprano steve at pearwood.info
Tue Jan 4 23:58:41 CET 2011


Dave Angel wrote:

> One hazard is if the string the user inputs has any regex special 
> characters in it.  If it's anything but letters and digits you probably 
> want to escape it before combining it with your \\b strings.

It is best to escape any user-input before passing it to regex 
regardless. The re.escape function will do the right thing whether the 
string is all letters and digits or not.

 >>> re.escape("dev")
'dev'
 >>> re.escape("dev+")
'dev\\+'


-- 
Steven




More information about the Tutor mailing list