Question

Recently I've been working on a PyQt program. In the beginning I used python re module to process the regex, but the transformation between python string and QString makes me confused. So I tried to change QRegExp.

However, I want to use the IGNORECASE, MULTILINE, DOTALL of the python re. I've found the QRegExp.setCaseSensitivity() to replace the re.I, but I can't find the rest two features. Can anybody help?

Or tell me how to transform the QString to python string? Both the regex pattern and data are input by the user, so their types are QString.

Was it helpful?

Solution

Qt's QRegExp class is perfectly okay, but python's re module is much more flexible. And the same goes for string handling in general - if you need to do a lot of string processing in a PyQt program, it's often best to do it all in python if you can.

Converting a QString is quite easy. In python2, you just need to do:

string = unicode(qstring)

And there's usually no need to convert it back: any Qt api that requires a QString will be automatically converted from a python string by PyQt.

A more general solution for QString conversion is to do it on a global basis using the sip module (which should already be available wherever PyQt is installed). This needs to be done at the beginning of your PyQt program, before any of the PyQt modules are imported, like this:

import sip
sip.setapi('QString', 2)

from PyQt4 import QtCore, QtGui

After doing this, any Qt API that normally returns a QString will return a python unicode object instead. For more in this feature, see Selecting Incompatible APIs in the PyQt docs.

Although it may initially require a little more work, taking this route is probably best in the long-run. For one thing, it will make porting to python3 easier if you should ever need to do so. For python3, the version 2 API is selected by default, so the above setapi step would not be needed.

OTHER TIPS

Just to post an answer on this side of things...

In most cases I've played with, the DOTALL "(.*?)" approach doesn't seem to match anything when it comes to QRegExp().

However, here's something I use that works in almost all my cases for matching triple-quoted strings:

single: "[^#][uUbB]?[rR]?(''')[^']*(''')"
double: '[^#][uUbB]?[rR]?(""")[^"]*(""")'

The only cases that don't work: ''' ' ''' and """ " """.
best thing I could find for now...

EDIT:
if I don't have those as the stop characters, it continues to match through the rest of the document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top