1. 程式人生 > 程式設計 >QString使用正則操作的介面實現

QString使用正則操作的介面實現

介紹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在字串中匹配的次數。

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,QRegularExpressionMatch *rmatch) const

lastIndexOf

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

int lastIndexOf(QRegExp &rx,int from = -1) const
int lastIndexOf(const QRegularExpression &re,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 ®,int start,int end = -1,QString::SectionFlags flags = SectionDefault) const
QString section(const QRegularExpression &re,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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。