diff --git a/autotests/krandomtest.cpp b/autotests/krandomtest.cpp --- a/autotests/krandomtest.cpp +++ b/autotests/krandomtest.cpp @@ -26,7 +26,7 @@ #include #include -#include +#include #include #include #include @@ -108,11 +108,12 @@ void KRandomTest::test_randomString() { const int desiredLength = 12; - const QRegExp outputFormat(QStringLiteral("[A-Za-z0-9]+")); - QString testString = KRandom::randomString(desiredLength); + const QString testString = KRandom::randomString(desiredLength); + const QRegularExpression outputFormat(QRegularExpression::anchoredPattern(QStringLiteral("[A-Za-z0-9]+"))); + const QRegularExpressionMatch match = outputFormat.match(testString); QCOMPARE(testString.length(), desiredLength); - QVERIFY(outputFormat.exactMatch(testString)); + QVERIFY(match.hasMatch()); } void KRandomTest::test_KRS() diff --git a/src/lib/io/kfileutils.cpp b/src/lib/io/kfileutils.cpp --- a/src/lib/io/kfileutils.cpp +++ b/src/lib/io/kfileutils.cpp @@ -20,6 +20,7 @@ #include #include +#include QString KFileUtils::suggestName(const QUrl &baseURL, const QString &oldName) { @@ -45,13 +46,14 @@ basename = oldName.left(oldName.length() - nameSuffix.length()); } - // check if (number) exists from the end of the oldName and increment that number - QRegExp numSearch(QStringLiteral("\\(\\d+\\)")); - int start = numSearch.lastIndexIn(oldName); - if (start != -1) { - QString numAsStr = numSearch.cap(0); - QString number = QString::number(numAsStr.midRef(1, numAsStr.size() - 2).toInt() + 1); - basename = basename.leftRef(start) + QLatin1Char('(') + number + QLatin1Char(')'); + // check if (number) exists at the end of the oldName and increment that number + const QRegularExpression re(QStringLiteral("\\((\\d+)\\)")); + QRegularExpressionMatch rmatch; + oldName.lastIndexOf(re, -1, &rmatch); + if (rmatch.hasMatch()) { + const int currentNum = rmatch.captured(1).toInt(); + const QString number = QString::number(currentNum + 1); + basename.replace(rmatch.capturedStart(1), rmatch.capturedLength(1), number); } else { // number does not exist, so just append " (1)" to filename basename += QLatin1String(" (1)"); diff --git a/src/lib/text/kmacroexpander_unix.cpp b/src/lib/text/kmacroexpander_unix.cpp --- a/src/lib/text/kmacroexpander_unix.cpp +++ b/src/lib/text/kmacroexpander_unix.cpp @@ -24,7 +24,7 @@ #include #include -#include +#include namespace KMacroExpander { @@ -114,10 +114,10 @@ } if (state.dquote) { rsts = rst.join(QLatin1Char(' ')); - rsts.replace(QRegExp(QStringLiteral("([$`\"\\\\])")), QStringLiteral("\\\\1")); + rsts.replace(QRegularExpression(QStringLiteral("([$`\"\\\\])")), QStringLiteral("\\\\1")); } else if (state.current == dollarquote) { rsts = rst.join(QLatin1Char(' ')); - rsts.replace(QRegExp(QStringLiteral("(['\\\\])")), QStringLiteral("\\\\1")); + rsts.replace(QRegularExpression(QStringLiteral("(['\\\\])")), QStringLiteral("\\\\1")); } else if (state.current == singlequote) { rsts = rst.join(QLatin1Char(' ')); rsts.replace(QLatin1Char('\''), QLatin1String("'\\''")); diff --git a/src/lib/text/kstringhandler.cpp b/src/lib/text/kstringhandler.cpp --- a/src/lib/text/kstringhandler.cpp +++ b/src/lib/text/kstringhandler.cpp @@ -24,6 +24,7 @@ #include // random() #include // for the word ranges +#include #include #include #include @@ -166,24 +167,12 @@ QString KStringHandler::tagUrls(const QString &text) { - /*static*/ QRegExp urlEx(QStringLiteral("(www\\.(?!\\.)|(fish|(f|ht)tp(|s))://)[\\d\\w\\./,:_~\\?=&;#@\\-\\+\\%\\$\\(]+[\\d\\w/\\)]")); - QString richText(text); - int urlPos = 0, urlLen; - while ((urlPos = urlEx.indexIn(richText, urlPos)) >= 0) { - urlLen = urlEx.matchedLength(); - QString href = richText.mid(urlPos, urlLen); - // Qt doesn't support (?<=pattern) so we do it here - if ((urlPos > 0) && richText[urlPos - 1].isLetterOrNumber()) { - urlPos++; - continue; - } - // Don't use QString::arg since %01, %20, etc could be in the string - QString anchor = QLatin1String("") + href + QLatin1String(""); - richText.replace(urlPos, urlLen, anchor); - - urlPos += anchor.length(); - } + const QRegularExpression urlEx(QStringLiteral("(www\\.(?!\\.)|(fish|ftp|http|https)://[\\d\\w\\./,:_~\\?=&;#@\\-\\+\\%\\$\\(\\)]+)")); + // the reference \1 is going to be replaced by the matched url + const QLatin1String regexBackRef(QLatin1String("\\1")); + const QString anchor = QLatin1String("") + regexBackRef + QLatin1String(""); + richText.replace(urlEx, anchor); return richText; } diff --git a/src/lib/util/klistopenfilesjob_unix.cpp b/src/lib/util/klistopenfilesjob_unix.cpp --- a/src/lib/util/klistopenfilesjob_unix.cpp +++ b/src/lib/util/klistopenfilesjob_unix.cpp @@ -23,6 +23,7 @@ #include "klistopenfilesjob.h" #include #include +#include class KListOpenFilesJobPrivate { @@ -65,7 +66,7 @@ return; } const QString out(QString::fromLocal8Bit(lsofProcess.readAll())); - QStringList pidList = out.split(QRegExp(QStringLiteral("\\s+")), QString::SkipEmptyParts); + QStringList pidList = out.split(QRegularExpression(QStringLiteral("\\s+")), QString::SkipEmptyParts); pidList.removeDuplicates(); for (const auto& pidStr : qAsConst(pidList)) { qint64 pid = pidStr.toLongLong(); diff --git a/src/lib/util/kshell_win.cpp b/src/lib/util/kshell_win.cpp --- a/src/lib/util/kshell_win.cpp +++ b/src/lib/util/kshell_win.cpp @@ -23,6 +23,7 @@ #include "kshell.h" #include "kshell_p.h" +#include #include #include #include @@ -269,7 +270,7 @@ // Escape quotes. Preceding backslashes are doubled. // Note that the remaining string is not quoted. QString ret(arg); - ret.replace(QRegExp(QLatin1String("(\\\\*)\"")), QLatin1String("\\1\\1\\^\"")); + ret.replace(QRegularExpression(QStringLiteral("(\\\\*)\"")), QStringLiteral("\\1\\1\\^\"")); ret.replace(QLatin1Char('%'), PERCENT_ESCAPE); return ret; }