substring and regular expression

Kent Johnson kent37 at tds.net
Thu Aug 4 09:35:31 EDT 2005


borges2003xx at yahoo.it wrote:
> i have read
> finding sublist
> http://groups.google.it/group/comp.lang.python/browse_thread/thread/50b09a0aca285256/5156ada81fc9358a?hl=it#5156ada81fc9358a
> 
> 
> the problem was in a string to find if we have two substring non
> overlapping of lenght al least 4
> 
> it was done by
> r=re.compile(r'(?P<seq>.{4,}).*(?P=seq)')
> 
> my knowhow on re is null
> there is a way in re for ask if a substring is the reverse of the
> otherone? First seq is second seq in reverse order

For a fixed-length substring you can use brute force. Here is a regex that matches a string containing a four-character substring and its reverse:
(?P<s1>.)(?P<s2>.)(?P<s3>.)(?P<s4>.).*(?P=s4)(?P=s3)(?P=s2)(?P=s1)

You might want to look at the Regular Expression HOW-TO document:
http://www.amk.ca/python/howto/regex/

Kent



More information about the Python-list mailing list