Simple regex with whitespaces

James Stroud jstroud at mbi.ucla.edu
Mon Sep 11 00:36:42 EDT 2006


mathieu.malaterre at gmail.com wrote:
> Hello,
> 
>   I cannot figure out a way to find a regular expression that would
> match one and only one of these two strings:
> 
> s1 = '                              how are you'
> s2 = '    hello world               how are you'
> 
>   All I could come up with was:
> patt = re.compile('^[ ]*([A-Za-z]+)[  ]+([A-Za-z]+)$')
> 
>   Which of course does not work. I cannot express the fact: sentence
> have 0 or 1 whitespace, separation of group have two or more
> whitespaces.
> 
> Any suggestion ? Thanks a bunch !
> Mathieu
> 


py> import re
py> s1 = '                              how are you'
py> s2 = '    hello world               how are you'
py> s3 = 'group here    now here but not here   but now here'
py> patt_2plus = re.compile(r'(?:(?:\S+(?:\s|$))+(?:\s+|$)){2,}')
py> patt_3plus = re.compile(r'(?:(?:\S+(?:\s|$))+(?:\s+|$)){3,}')

positive tests:

py> patt_2plus.search(s2).group(0)
'hello world               how are you'
py> patt_2plus.search(s3).group(0)
'group here    now here but not here   but now here'
py> patt_3plus.search(s3).group(0)
'group here    now here but not here   but now here'



negative tests:

py> patt_3plus.search(s2).group(0)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: 'NoneType' object has no attribute 'group'
py> patt_3plus.search(s1).group(0)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: 'NoneType' object has no attribute 'group'
py> patt_2plus.search(s1).group(0)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
AttributeError: 'NoneType' object has no attribute 'group' 



James

-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list