溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

QString使用正則操作的接口實現(xiàn)

發(fā)布時間:2020-09-02 11:55:02 來源:腳本之家 閱讀:256 作者:Qt君 欄目:編程語言

介紹QString使用正則操作的接口。

Qt 5.0引入QRegularExpression,相比于QRegExp,前者修復了很多bugs,功能上也是兼容于QRegExp。推薦使用QRegularExpression。

contains

正則表達式rx是否與字符串中的某個地方匹配,匹配返回true,否則返回false。

bool contains(const QRegExp &rx) const
bool contains(QRegExp &rx) const
bool contains(const QRegularExpression &re) const
bool contains(const QRegularExpression &re, QRegularExpressionMatch *match) const

count

返回正則表達式rx在字符串中匹配的次數(shù)。

int count(const QRegExp &rx) const
int count(const QRegularExpression &re) const

indexOf

返回字符串中正則表達式rx的第一個匹配項的索引位置,從索引位置向前搜索。如果rx在任何地方都不匹配,則返回-1。

int indexOf(QRegExp &rx, int from = 0) const
int indexOf(const QRegularExpression &re, int from = 0) const
int indexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const

lastIndexOf

返回字符串中正則表達式rx最后一個匹配項的索引位置,從索引位置向后搜索。如果rx在任何地方都不匹配,則返回-1。

int lastIndexOf(QRegExp &rx, int from = -1) const
int lastIndexOf(const QRegularExpression &re, int from = -1) const
int lastIndexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const

remove

移除字符串中符合正則表達式rx的匹配,并返回對該字符串的引用。

QString &remove(const QRegExp &rx)
QString &remove(const QRegularExpression &re)

replace

將字符串中符合正則表達式rx就替換為after字符串,并返回對該字符串的引用。

QString &replace(const QRegExp &rx, const QString &after)
QString &replace(const QRegularExpression &re, const QString &after)

section

將字符串由正則表達式來分割字段序列。

QString section(const QRegExp &reg, int start, int end = -1, QString::SectionFlags flags = SectionDefault) const
QString section(const QRegularExpression &re, int start, int end = -1, QString::SectionFlags flags = SectionDefault) const

split

將字符串拆分為正則表達式rx匹配的子字符串,并返回這些字符串的列表。

QStringList split(const QRegExp &rx, QString::SplitBehavior behavior = KeepEmptyParts) const
QStringList split(const QRegularExpression &re, QString::SplitBehavior behavior = KeepEmptyParts) const

splitRef

將字符串拆分為正則表達式rx匹配的子字符串引用,并返回這些字符串的列表。

QVector<QStringRef> splitRef(const QRegExp &rx, QString::SplitBehavior behavior = KeepEmptyParts) const
QVector<QStringRef> splitRef(const QRegularExpression &re, QString::SplitBehavior behavior = KeepEmptyParts) const

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI