[Tutor] regex question

Dave Angel davea at ieee.org
Tue Jan 4 23:38:16 CET 2011


On 01/-10/-28163 02:59 PM, Richard D. Moores wrote:
> On Tue, Jan 4, 2011 at 11:57, Richard D. Moores<rdmoores at gmail.com>  wrote:
>> On Tue, Jan 4, 2011 at 10:41, Richard D. Moores<rdmoores at gmail.com>  wrote:
>>> Please see http://tutoree7.pastebin.com/z9YeSYRw . I'm actually
>>> searching RTF files, not TXT files.
>>>
>>> I want to modify this script to handle searching on a word. So what,
>>> for example, should line 71 be?
>>
>> OK, I think I've got it.
>>
>> in place of lines 66-75 I now have
>>
>> search =nput("first search string: ")
>>     search =\\b" + search + "\\b"
>>     if not search:
>>         print("Bye")
>>         sys.exit()
>>     elif search[0] != ':
>>         p =e.compile(search, re.I)
>>     else:
>>         p =e.compile(search)
>
> Oops. That should be
>
> search =nput("first search string: ")
>      if not search:
>          print("Bye")
>          sys.exit()
>      elif search[0] != ':
>          search =\\b" + search + "\\b"
>          p =e.compile(search, re.I)
>      else:
>          search =\\b" + search + "\\b"
>          p =e.compile(search)
>
> Dick
>
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.

DaveA



More information about the Tutor mailing list