Regular Expressions

Jens Thiede jens.news at webgear.co.za
Sat Aug 14 13:01:19 EDT 2004


Right, but if you add (.*) then you get:

 >>> obj=re.compile(r'<t>(.*?)</t>', re.S|re.I)
 >>> obj.split('def<T>abc\n def</T>ghi jkl\nmno\n<t>abc</t>haha')
['def', 'abc\n def', 'ghi jkl\nmno\n', 'abc', 'haha']
 >>> obj.findall('def<T>abc\n def</T>ghi jkl\nmno\n<t>abc</t>haha')
['abc\n def', 'abc']

*Note*: 'abc\n def' appears twice.

Any suggestions?

Jens.

Isaac To wrote:
>>>>>>"Jens" == Jens Thiede <jens.news at webgear.co.za> writes:
> 
> 
>     Jens> What is the easiest way to generate a list that contains all
>     Jens> the remnants of re.findall (the inverse of the following).
> 
> You mean, complement?
> 
>     Jens> re.split doesn't seem to work quite well
> 
> Why?
> 
> 
>>>>obj=re.compile(r'<t>.*?</t>', re.S|re.I)
>>>>obj.split('def<T>abc\n def</T>ghi jkl\nmno\n<t>abc</t>haha')
> 
> ['def', 'ghi jkl\nmno\n', 'haha']
> 
>>>>obj.findall('def<T>abc\n def</T>ghi jkl\nmno\n<t>abc</t>haha')
> 
> ['<T>abc\n def</T>', '<t>abc</t>']
> 
> 
>>>>obj=re.compile(r'<t>.*</t>', re.S|re.I)
>>>>obj.split('def<T>abc\n def</T>ghi jkl\nmno\n<t>abc</t>haha')
> 
> ['def', 'haha']
> 
>>>>obj.findall('def<T>abc\n def</T>ghi jkl\nmno\n<t>abc</t>haha')
> 
> ['<T>abc\n def</T>ghi jkl\nmno\n<t>abc</t>']
> 
> Looks like it is doing the job, right?
> 
> Regards,
> Isaac.



More information about the Python-list mailing list