当前位置:网站首页>Regular expressions in QT
Regular expressions in QT
2022-06-13 04:53:00 【qq_ thirty-nine million two hundred and eighty thousand seven h】
@[TOC]( Short step A thousand miles )
Qt Regular expressions in
Regular expressions (regular expression), Is a pattern of matching substrings in a text (pattern), It can be abbreviated as “regexp”,. One regexp It is mainly used in the following aspects :
verification : One regexp You can test whether a substring meets some criteria . for example , Is an integer or does not contain any spaces .
** Search for :** One regexp Provides more powerful pattern matching than simple substring matching . for example , Match words mail perhaps letter, Words don't match email perhaps letterbox.
** Find and replace :** One regexp You can replace all substrings in a string with a different string . for example , Use Mail To replace all... In a string M character , But if M Character followed by ail Do not replace when .
** String segmentation :** One regexp Can identify where to split the string . for example , Split tab isolated string .
Qt Medium QRegExp Class implements pattern matching using regular expressions .QRegExp In order to Perl Regular expression language based on , It fully supports Unicode.QRegExp The syntax rules in can use setPatternSyntax() Function to change
Qt Regular expressions
Regular expressions (regular expresion ): By the expression (expressions)、 quantifiers (quantifiers) And assertions (assertions). The simplest regular expression is just an expression . For example, match a character a,[a].
expression :
Express matching characters , For example, match a character [a]; character string "hello".
A set of characters can be enclosed in square brackets , for example [ABC] Will match a A Or a B Or a C, This can also be abbreviated as [A-C], So we have to match all the capital letters , You can use [A-Z].
quantifiers :
Specifies the number of occurrences of the expression that must be matched .
{m,n}: At least m Time , at most n Time
{0,1}: Match at most once
{1,}: At least 1 Time ,
Assertion :
Assertion in regexp Make some statements about the text , They don't match any characters .
for example : Request in string ”123“ Get numbers , Require ab initio matching , Match to the end of the string ,( That is, match the entire string ), So the expression :^ [0-9] {1,3} . break said “ ” and “ . Assertion “^” and “ . break said “” and “”, When ^ stay regexp As the first character in , It means this regexp You must match from the beginning of the string ; When $ stay regexp As the last character in , signify regexp Must match to the end of the string .
Expression combination :
take regexp Enclosed in brackets , namely (regexp1|regexp2) . Parentheses group expressions together , Can be in a more complex regexp As a component in ,
Use one regexp To match words “mail” perhaps “letter” One of them , But don't match words that contain these words , such as “email” and “letterbox”.
To match “mail”,regexp It can be written. m{1,1}a{1,1}i{1,1}l{1,1} , because {1,1} It can be omitted , So it can be abbreviated as mail .
You can use vertical lines below “|” To include another word , here “|” Express “ or ” It means . for fear of regexp Match extra words , It must be matched from the boundary of the word .
First of all, will regexp Enclosed in brackets , namely (mail|letter) . To force the beginning and end of the match to be on the word boundary , To put regexp Included in \b Word boundary assertion , namely \b(mail|letter)\b . This \b Assertion in regexp Match a position in , Not a character , The boundary of a word is any non word character , Like a space , New line , Or the beginning or end of a string .
Alternative symbol
Generally, some special symbols can be used to represent some common character groups and quantifiers .
Quantifier usage :
default , An expression will be automatically quantified as {1,1}, That means it should appear once . The usage of quantifiers is listed in the following table , among E Represents an expression , An expression can be a character , Or an abbreviation of a character set , Or a character set in square brackets , Or an expression in parentheses .
Assertion usage :
The assertions in regular expressions are shown in the following table , among E Represents an expression .
wildcard :
QRegExp Class also supports wildcards (Wildcard) matching .QRegExp Of setPatternSyntax() The function is used in regexp And wildcards . Wildcard matching is better than regexp Simple a lot , It has only four characteristics , As shown in the following table .
Regular expression examples
A regular expression is a search
Matching character a
QRegExp ex(“a”);
QString str(“a”);
QString str1(“c”);
qDebug() << ex.indexIn(str);
qDebug() << ex.indexIn(str1);
Output :
Match special character ’‘
'\' As a change of mind , That is, usually in the "“ The following characters are not interpreted in the original sense , Such as /b/ Matching character "b”, When b After adding the reverse diagonal bar in front of /\b/, Match the boundary of a word .
perhaps :
Restore regular expression function characters , Such as "“ Match the metacharacter before it 0 Times or times ,/a*/ Will match a,aa,aaa, added ”“ after ,/a*/ Will only match "a*”.
ex.setPattern(("*"));
str = “awd*”;
qDebug() << ex.indexIn(str);
ex.setPattern(("\\*")); // first ’\’ For the second ‘\’ Restore . Represents a match ‘*’ character
qDebug() << ex.indexIn(str);
Output :
Match string
str = “my name is kk”;
ex.setPattern((“nam”));
qDebug() << ex.indexIn(str);
ex.setPattern("\bname\b"); // Match word boundaries
qDebug() << ex.indexIn(str);
Output :
ex.setPattern("^my");
qDebug() << ex.indexIn(str);
ex.setPattern("^name"); // Match from beginning
qDebug() << ex.indexIn(str);
Output :
ex.setPattern("^my(?= )"); /// requirement my Followed by a space
qDebug() << ex.indexIn(str);
ex.setPattern("^my(?=name)");
qDebug() << ex.indexIn(str);
Output :
Regular expression find and replace
Find references :
QRegExp rx4("(\b\w+(?!\b)");
QString str4 = “name: tom tony marry”;
QStringList list;
int pos2 = 0;
while ((pos2 = rx4.indexIn(str4, pos2)) != -1)
{
list << rx4.cap(1); // The first captured text
pos2 += rx4.matchedLength(); // The length of the last matching string
}
qDebug() << list;
Output :
backreferences :
Can be in regexp Use captured text in , To represent the captured text , Use reverse references \n , among n from 1 Numbered starting , such as \1 It means the first captured text . for example , Use \b(\w+)\W+\1\b Query for repeated words in a string , This means matching a word boundary first , Followed by one or more word characters , Followed by one or more non word characters , This is followed by the same text as in the first preceding parenthesis , Followed by word boundaries .
Here is a capture group concept : Reference resources Regular basis —— Capture group (capture group).
tried , It seems impossible .
Replace :
QRegExp re ("(?:world)"); // Match not world part ?: Indicates non capture
QString str3 = “hello world”;
str3.replace(re,“tom”);
qDebug()<< str3;
Output :
Regular expression character verification
QString pattern(“.=.”);
QRegExp rx(pattern);
bool match = rx.exactMatch(“a=3″);
qDebug() << match; // True
match = rx.exactMatch(“a/2″);
qDebug() << match; // False
Regular expression string segmentation
QString strFrame = “hello world, tom sss”;
strFrame = strFrame.simplified(); // simplified Is a very useful function , You can remove the spaces at the beginning and end and the carriage return , The extra space in the middle is not missed .
QStringList sections = strFrame.split(QRegExp( “[ ,]+”)); // Remove non characters , Write the string into QStringList in
qDebug() << sections ;
Output :
边栏推荐
- Section 4 - arrays
- E - Lucky Numbers
- rust编程-链表:使用struct实现链表,使用堆合并k个升序链表,自定义Display
- 2022 oxidation process operation certificate examination question bank and simulation examination
- Four methods for judging JS data types and user-defined methods
- Develop go using vscode
- C#获取WebService接口的所有可调用方法[WebMethod]
- [try to hack] upload labs (temporarily write to 12)
- RMQ、LCA
- Force buckle 25 A group of K flipped linked lists
猜你喜欢

Cesium:cesiumlab makes image slices and loads slices
![[leetcode]- binary search](/img/7f/7d1f616c491c6fb0be93f591da6df1.png)
[leetcode]- binary search
![[LeetCode]-二分查找](/img/7f/7d1f616c491c6fb0be93f591da6df1.png)
[LeetCode]-二分查找

Embedded hardware - read schematic
![[try to hack] upload labs (temporarily write to 12)](/img/df/dbb78121f7428e25ffb73cfc43ce1b.png)
[try to hack] upload labs (temporarily write to 12)

Analysis on the usage, response and global delivery of provide/inject

Infinite cycle scrolling code Alibaba international station store decoration code base map scrolling black translucent display effect custom content decoration code full screen display

Solution to sudden font change in word document editing

2022 ICLR | CONTRASTIVE LEARNING OF IMAGE- AND STRUCTURE BASED REPRESENTATIONS IN DRUG DISCOVERY

PowerShell plus domain add computer module
随机推荐
Section 6 - pointers
A simple understanding of consistent hash
Section 3 - functions
ES6 learning
无限循环滚动代码阿里巴巴国际站店铺装修代码底图滚动黑色半透明显示效果自定义内容装修代码全屏显示
Use go to add massive data to MySQL
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
Leetcode game 297 (20220612)
RuoYi-Cloud启动教程(手把手图文)
[LeetCode]-滑动窗口
Trust programming - linked lists: use struct to implement linked lists, use heap to merge K ascending linked lists, and customize display
Test question bank and online simulation test for special operation certificate of construction scaffolder (special type of construction work) in 2022
Tita: Xinrui group uses one-to-one talk to promote the success of performance change
PowerShell plus domain add computer module
Stepping on a horse (one stroke)
【JS解决】leedcode 117. 填充每个节点的下一个右侧节点指针 II
How to implement a custom jdbc driver in only four steps?
CreateAnonymousThreadX给匿名线程传递参数
Powershell 加域 Add-Computer模块
[untitled]