diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2010-12-03 17:07:11 -0300 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2010-12-06 15:33:51 -0300 |
commit | ed1c4c72b91773f66a3a6c544cc5041e1d4c6c6d (patch) | |
tree | 880554ecdf798d9691a60b7e362e92b783e3c33b | |
parent | 2d595e22b00e6511cd0543ca6ddf329d65826079 (diff) | |
download | pyside-ed1c4c72b91773f66a3a6c544cc5041e1d4c6c6d.tar.gz pyside-ed1c4c72b91773f66a3a6c544cc5041e1d4c6c6d.tar.xz pyside-ed1c4c72b91773f66a3a6c544cc5041e1d4c6c6d.zip |
Added QRegExp.replace(QString, const char*) method.
The only way to search and replace using QRegExp is using the
QString::replace method. Since QString was removed, QRegExp now is
useful only to search stuff, but not replace. For this purpose the
QRegExp.replace method was added. The first argument is the string
that will be operated over, the second argument contains the
replacement, and the return value is a new modified Python string.
Unit tests and documentation for QRegExp.replace were added as well.
Reviewed by Hugo Parente <hugo.lima@openbossa.org>
Reviewed by Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r-- | PySide/QtCore/typesystem_core.xml | 35 | ||||
-rw-r--r-- | tests/QtCore/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/QtCore/qregexp_test.py | 20 |
3 files changed, 56 insertions, 0 deletions
diff --git a/PySide/QtCore/typesystem_core.xml b/PySide/QtCore/typesystem_core.xml index 6da4f64..5ed694c 100644 --- a/PySide/QtCore/typesystem_core.xml +++ b/PySide/QtCore/typesystem_core.xml @@ -1390,6 +1390,41 @@ </insert-template> </inject-code> </add-function> + <add-function signature="replace(QString, QString)" return-type="QString"> + <modify-argument index="1"> + <rename to="sourceString"/> + </modify-argument> + <modify-argument index="2"> + <rename to="after"/> + </modify-argument> + <inject-documentation format="target" mode="append"> + Replaces every occurrence of the regular expression in *sourceString* with *after*. + Returns a new Python string with the modified contents. For example: + + :: + + s = "Banana" + re = QRegExp("a[mn]") + s = re.replace(s, "ox") + # s == "Boxoxa" + + + For regular expressions containing capturing parentheses, occurrences of \1, \2, ..., in *after* + are replaced with rx.cap(1), cap(2), ... + + :: + + t = "A <i>bon mot</i>." + re = QRegExp("<i>([^<]*)</i>") + t = re.replace(t, "\\emph{\\1}") + # t == "A \\emph{bon mot}." + + </inject-documentation> + <inject-code class="target" position="beginning"> + %1.replace(*%CPPSELF, %2); + %PYARG_0 = %CONVERTTOPYTHON[QString](%1); + </inject-code> + </add-function> </value-type> <value-type name="QFileInfo"> <extra-includes> diff --git a/tests/QtCore/CMakeLists.txt b/tests/QtCore/CMakeLists.txt index b73a1ba..40b8386 100644 --- a/tests/QtCore/CMakeLists.txt +++ b/tests/QtCore/CMakeLists.txt @@ -49,6 +49,7 @@ PYSIDE_TEST(qobject_tr_as_instance_test.py) PYSIDE_TEST(qpoint_test.py) PYSIDE_TEST(qprocess_test.py) PYSIDE_TEST(qrect_test.py) +PYSIDE_TEST(qregexp_test.py) PYSIDE_TEST(qresource_test.py) PYSIDE_TEST(qsize_test.py) PYSIDE_TEST(qslot_object_test.py) diff --git a/tests/QtCore/qregexp_test.py b/tests/QtCore/qregexp_test.py new file mode 100644 index 0000000..5499185 --- /dev/null +++ b/tests/QtCore/qregexp_test.py @@ -0,0 +1,20 @@ +#!/usr/bin/python + +import unittest +from PySide.QtCore import QRegExp + +class QRegExpTest(unittest.TestCase): + + def testReplace1(self): + re = QRegExp('a[mn]') + string = re.replace('Banana', 'ox') + self.assertEqual(string, 'Boxoxa') + + def testReplace2(self): + re = QRegExp('<i>([^<]*)</i>') + string = re.replace('A <i>bon mot</i>.', '\\emph{\\1}') + self.assertEqual(string, 'A \\emph{bon mot}.') + +if __name__ == '__main__': + unittest.main() + |