QString使用正則操作的介面
- 2019 年 12 月 12 日
- 筆記
介紹QString使用正則操作的介面。
Qt 5.0引入QRegularExpression,相比於QRegExp,前者修復了很多bugs,功能上也是兼容於QRegExp。推薦使用QRegularExpression。
contains
- 正則表達式rx是否與字元串中的某個地方匹配,匹配返回true,否則返回false。
bool contains(const QRegExp &rx) constbool contains(QRegExp &rx) constbool contains(const QRegularExpression &re) constbool contains(const QRegularExpression &re, QRegularExpressionMatch *match) const
count
- 返回正則表達式rx在字元串中匹配的次數。
int count(const QRegExp &rx) constint count(const QRegularExpression &re) const
indexOf
- 返回字元串中正則表達式rx的第一個匹配項的索引位置,從索引位置向前搜索。如果rx在任何地方都不匹配,則返回-1。
int indexOf(QRegExp &rx, int from = 0) constint indexOf(const QRegularExpression &re, int from = 0) constint indexOf(const QRegularExpression &re, int from, QRegularExpressionMatch *rmatch) const
lastIndexOf
- 返回字元串中正則表達式rx最後一個匹配項的索引位置,從索引位置向後搜索。如果rx在任何地方都不匹配,則返回-1。
int lastIndexOf(QRegExp &rx, int from = -1) constint lastIndexOf(const QRegularExpression &re, int from = -1) constint 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 ®, int start, int end = -1, QString::SectionFlags flags = SectionDefault) constQString 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) constQStringList split(const QRegularExpression &re, QString::SplitBehavior behavior = KeepEmptyParts) const
splitRef
- 將字元串拆分為正則表達式rx匹配的子字元串引用,並返回這些字元串的列表。
QVector<QStringRef> splitRef(const QRegExp &rx, QString::SplitBehavior behavior = KeepEmptyParts) constQVector<QStringRef> splitRef(const QRegularExpression &re, QString::SplitBehavior behavior = KeepEmptyParts) const