[QT learning] QRegExp class regular expressions (understand in one article)


foreword

   When using the edit box to input content, in order to prevent users from entering irrelevant information and prevent SQL injection, we can use QRegExp class regular expressions to limit the input content of the edit box. In this article, the author will share how to use the regular expressions of the QRegExp class.


1. Introduction to QRegExp

  • The QRegExp class is a regular expression in QT, consisting of expressions, quantifiers and assertions.

  • There are four main functions: validity check, search, replace, and string segmentation, as follows.

  (1) Validity check, check whether the string meets certain requirements.

  (2) Search, which provides a more powerful matching model than the QString class.

  (3) Replacement, replace the strings that meet or do not meet the requirements in the string.

  (4) String segmentation, use regular expressions to split the required parts of the string

2. Metacharacters and wildcard patterns

1. Metacharacters

  • Metacharacters are a class of regular expressions in QRegExp, which represent one or more constant expressions
metacharacter effect example
. matches any single character 1.2, which may be 1 followed by any character, followed by 2
^ match the beginning of the string ^12, can be 123, but not 312
$ match end of string 12$, can be 312, but cannot be 123
[] matches any character entered inside the parentheses [123], can be 1, 2 or 3
* matches any number of leading characters 1*2, which can be any number of 1s (even none), followed by a 2
+ matches at least one leading character 1+2, must be one or more 1s, followed by a 2
? matches a leading character or is empty 1?2, can be 2 or 12

2. Wildcard mode

  • The QRegExp class supports wildcard matching, and the wildcard pattern is simpler than RegExp
  • In wildcard mode, only ?, *, and []3 characters can be used, and their functions change
  • QRegExp::setPatternSyntax(QRegExp::Wildcard)Metacharacters can be set to globbing patterns by
wildcard effect example
? matches any single character 1?2, can be 1, followed by any single character, followed by 2
* matches any sequence of characters 1*2, which can be 1, followed by any number of characters, followed by a 2
[] matches a defined set of characters [a-z]Can match any character between a and z; [^a]match characters other than lowercase a.

3. QRegExp structure and method

1. Default constructor

  • Generate an empty regular expression object, the function declaration is as follows.
	QRegExp();	

2. Schema constructor

  • Generate a regular expression object of the specified matching pattern, the function declaration is as follows.
	QRegExp(const QString &pattern, Qt::CaseSensitivity cs = Qt::CaseSensitive, PatternSyntax syntax = RegExp)

3. isValid() function

  • Determine whether the regular expression is legal, return true if it is legal, otherwise return false.
	QRegExp exp1("[a-z]");  
	bool valid=exp1.isValid();	//返回true  
	QRegExp exp1("[a-z");  
	bool valid=exp1.isValid();	//返回false 

4. caseSensitivity() function

  • Determine whether the regular expression is case-sensitive.
	Qt::CaseSensitivity caseSensitivity() const;

5. cap() and capturedTexts() functions

  • The former gets every item captured, index starts from 1, and the latter gets the entire capture list.
	QString pattern() const;
	QStringList capturedTexts() const;

6. indexIn() function

  • Match, return index if successful, return -1 if unsuccessful.
	indexIn(const QString &str, int offset = 0, CaretMode caretMode = CaretAtZero) const;

7. exactMatch() function,

  • Whether the whole string matches, return true or false.
	exactMatch(const QString &str) const;

7. The matchedLength() function

  • Returns the length of the matched string.
	int matchedLength() const;

8. pattern() function

  • Get the regular expression itself.
	QString pattern() const;

9. Construction and method examples

  • The usage examples of some methods of QRegExp class are as follows:
	
	QRegExp expression;									//定义QRegExp 

	expression.setPatternSyntax(QRegExp::RegExp);		//设置为RegExp模式
	
	expression.setCaseSensitivity(Qt::CaseSensitive); 	//设置大小写敏感
	
	expression.setPattern(QString("^[A-Za-z0-9]+$")); 	//匹配所有大小写字母和数字组成的字符串
	
	QString s = "kojad@0123";							//定义字符串
	
	expression.exactMatch(s); 							//返回的值为false,因为s中含有@字符
	

4. Examples of common regular expressions

  • The following lists some regular expression meanings, common ways of writing, and specific examples.

1. Commonly used regular expressions and examples 1


	\r, \n 代表 回车和换行符
	
	\t 制表符
	
	\\ 代表 "\" 本身
	
	\^ 匹配 ^ 符号本身
	
	\$ 匹配 $ 符号本身
	
	. 匹配除了换行符以外的任意字符
	
	\w 匹配字母、数字、下划线、汉字
	
	\s 匹配任意的空白符
	
	\b 单词的开始或结尾  
	
	\~ 匹配字符串的开始  
	
	$ 匹配字符串的结束
	
	[]  包含一系列字符

	[^]  包含之外一系列字符
	1"\ba\w*\b" ,匹配以字母a开头的单词,先是开始(\b),然后是字母a,然后是任意数量的字母或数字(\w*),最后是结束(\b).
	2"\d+",匹配1个或更多连续的数字。+匹配重复1次或更多次.
	3"\b\w{6}\b", 匹配刚好6个字符的单词.
	4"[ab5@]": 匹配 "a""b""5""@".
5"[^abc]": 包含abc之外的任意字符.
6"[f-k]": f-k之间的任意字符.
	

2. Commonly used regular expressions and examples 2


	{
    
    n} 表达式重复n次,比如:"\w{2}" 相当于 "\w\w""a{5}" 相当于 "aaaaa".
	
	{
    
    m,n} 表达式至少重复m次,最多重复n次,比如:"ba{1,3}"可以匹配 "ba""baa""baaa".
	
	{
    
    m,} 表达式至少重复m次,比如:"\w\d{2,}"可以匹配 "a12","_456","M12344".
	
	? 表达式0次或者1次,相当于 {
    
    0,1},比如:"a[cd]?"可以匹配 "a","ac","ad"
	
	+ 表达式至少出现1次,相当于 {
    
    1,},比如:"a+b"可以匹配 "ab","aab","aaab".  
	
	* 表达式不出现或出现任意次,相当于 {
    
    0,},比如:"\^*b"可以匹配 "b","^^^b".
1"\w{2}"相当于 "\w\w""a{5}" 相当于 "aaaaa".
	2"ba{1,3}"可以匹配 "ba""baa""baaa".
	3"\w\d{2,}"可以匹配 "a12","_456","M12344".
	4"a[cd]?"可以匹配 "a","ac","ad".
	5"a+b"可以匹配 "ab","aab","aaab".
	6"\^*b"可以匹配 "b","^^^b".
	7"[ab5@]" 可以匹配 "a""b""5""@".
	8"[f-k]" 可以匹配 "f"~"k" 之间的任意一个字母.
	9"[^abc]" 可以匹配 "a","b","c" 之外的任意一个字符.
	10"[^A-F0-3]" 可以匹配 "A"~"F","0"~"3" 之外的任意一个字符.
	

Summarize

   The above is all the content of [QT Learning] QRegExp regular expressions, I hope everyone can gain something after reading! Originality is not easy, please indicate the source for reprinting, if there is any error in the article, readers are welcome to leave a message to correct and criticize!

insert image description here

Guess you like

Origin blog.csdn.net/qq_59134387/article/details/127182436