[Tutor] regex problem with colon

Tim Johnson tim at johnsons-web.com
Fri Aug 7 05:37:06 CEST 2009


* Kent Johnson <kent37 at tds.net> [090806 18:31]:
> On Thu, Aug 6, 2009 at 8:47 PM, Tim Johnson<tim at johnsons-web.com> wrote:
> > using python 2.5.
> > I'm having a problem with including a colon as part of a substring
> > bounded by whitespace or beginning of line.
> > Here's an example:
> > p = re.compile(r'\bcc:\b',re.IGNORECASE)
> >>>> res = p.findall('malicious cc: here CC: there')
> >>>> res
> > []
> > ## Darn! I'd hope that the 'cc:' and 'CC:' substrings would be
> > found. So how to compose the expression correctly?
> 
> The problem is that : is not a "word" character, so there is no word
> boundary between : and space for \b to match. How about this:
> In [9]: p = re.compile(r'\bcc:',re.IGNORECASE)
 
  Yes. You nailed it Kent.
  I had grokked the logic but not entirely the syntax.
  I'm looking thru docs now. Just curious .. is there a flag that
  enables adding a ':' to the internal list of "word" characters?

> In [10]: p.findall('malicious cc: here CC: there')
> Out[10]: ['cc:', 'CC:']

thanks again.
-- 
Tim 
tim at johnsons-web.com
http://www.akwebsoft.com


More information about the Tutor mailing list