Regular Expressions

J

Jens Thiede

What is the easiest way to generate a list that contains all the
remnants of re.findall (the inverse of the following).
>>> file = open("index.html", "r")
>>> data = file.read()
>>> patern = re.compile("<title>(.*)</title>", re.S | re.I)
>>> patern.findall(data) ['Damn Small Expansions!']
>>> file.close()

re.split doesn't seem to work quite well, but then, I'm probably using
it wrong.

Any suggestions?

Jens.
 
I

Isaac To

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')
[' said:
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.
 
J

Jens Thiede

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 said:
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?


['def', 'ghi jkl\nmno\n', 'haha']

['def', 'haha']

['<T>abc\n def</T>ghi jkl\nmno\n<t>abc</t>']

Looks like it is doing the job, right?

Regards,
Isaac.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top