⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qregexp.3qt

📁 linux下GUI编程工具qt的在线连接帮助手册
💻 3QT
📖 第 1 页 / 共 3 页
字号:
'\" t.TH QRegExp 3qt "11 October 2001" "Trolltech AS" \" -*- nroff -*-.\" Copyright 1992-2001 Trolltech AS.  All rights reserved.  See the.\" license file included in the distribution for a complete license.\" statement..\".ad l.nh.SH NAMEQRegExp \- Pattern matching using regular expressions.PP\fC#include <qregexp.h>\fR.PP.SS "Public Members".in +1c.ti -1c.BI "\fBQRegExp\fR ()".br.ti -1c.BI "\fBQRegExp\fR ( const QString & pattern, bool caseSensitive = TRUE, bool wildcard = FALSE )".br.ti -1c.BI "\fBQRegExp\fR ( const QRegExp & rx )".br.ti -1c.BI "\fB~QRegExp\fR ()".br.ti -1c.BI "QRegExp & \fBoperator=\fR ( const QRegExp & rx )".br.ti -1c.BI "bool \fBoperator==\fR ( const QRegExp & rx ) const".br.ti -1c.BI "bool \fBoperator!=\fR ( const QRegExp & rx ) const".br.ti -1c.BI "bool \fBisEmpty\fR () const".br.ti -1c.BI "bool \fBisValid\fR () const".br.ti -1c.BI "QString \fBpattern\fR () const".br.ti -1c.BI "void \fBsetPattern\fR ( const QString & pattern )".br.ti -1c.BI "bool \fBcaseSensitive\fR () const".br.ti -1c.BI "void \fBsetCaseSensitive\fR ( bool sensitive )".br.ti -1c.BI "bool \fBwildcard\fR () const".br.ti -1c.BI "void \fBsetWildcard\fR ( bool wildcard )".br.ti -1c.BI "bool \fBminimal\fR () const".br.ti -1c.BI "void \fBsetMinimal\fR ( bool minimal )".br.ti -1c.BI "bool \fBexactMatch\fR ( const QString & str ) const".br.ti -1c.BI "int match ( const QString & str, int index = 0, int * len = 0, bool indexIsStart = TRUE ) const  \fI(obsolete)\fR".br.ti -1c.BI "int \fBsearch\fR ( const QString & str, int start = 0 ) const".br.ti -1c.BI "int \fBsearchRev\fR ( const QString & str, int start = -1 ) const".br.ti -1c.BI "int \fBmatchedLength\fR () const".br.ti -1c.BI "QStringList \fBcapturedTexts\fR ()".br.ti -1c.BI "QString \fBcap\fR ( int nth = 0 )".br.ti -1c.BI "int \fBpos\fR ( int nth = 0 )".br.in -1c.SH DESCRIPTIONThe QRegExp class provides pattern matching using regular expressions..PP.PPRegular expressions, or "regexps", provide a way to find patterns within text. This is useful in many contexts, for example:.PP<ol type=1>.IP 1\fIValidation\fR. A regexp can be used to check whether a piece of text meets some criteria, e.g. is an integer or contains no whitespace..IP 2\fISearching\fR. Regexps provide a much more powerful means of searching text than simple string matching does. For example we can create a regexp which says "find one of the words 'mail', 'letter' or 'correspondence' but not any of the words 'email', 'mailman' 'mailer', 'letterbox' etc.".IP 3\fISearch and Replace.\fR A regexp can be used to replace a pattern with a piece of text, for example replace all occurrences of '&' with '&amp;' except where the '&' is already followed by 'amp;'..IP 4\fIString Splitting.\fR A regexp can be used to identify where a string should be split into its component fields, e.g. splitting tab-delimited strings..PPWe present a very brief introduction to regexps, a description of Qt's regexp language, some code examples, and finally the function documentation. QRegExp is modeled on Perl's regexp language, and also fully supports Unicode. QRegExp may also be used in the weaker 'wildcard' (globbing) mode which works in a similar way to command shells. A good text on regexps is \fIMastering Regular Expressions: Powerful Techniques for Perl and Other Tools\fR by Jeffrey E. Friedl, ISBN 1565922573..PPExperienced regexp users may prefer to skip the introduction and go directly to the relevant information..PP.TPIntroduction.TPCharacters and Abbreviations for Sets of Characters.TPSets of Characters.TPQuantifiers.TPCapturing Text.TPAssertions.TPWildcard Matching (globbing).TPNotes for Perl Users.TP Code Examples.SH "Introduction"Regexps are built up from expressions, quantifiers and assertions. The simplest form of expression is simply a character, e.g. \fBx\fR or \fB5\fR. An expression can also be a set of characters. For example, \fB[ABCD]\fR, will match an \fBA\fR or a \fBB\fR or a \fBC\fR or a \fBD\fR. As a shorthand we could write this as \fB[A-D]\fR. If we want to match any of the captital letters in the English alphabet we can write \fB[A-Z]\fR. A quantifier tells the regexp engine how many occurrences of the expression we want, e.g. \fBx{1,1}\fR means match an \fBx\fR which occurs at least once and at most once. We'll look at assertions and more complex expressions later..PPNote that in general regexps cannot be used to check for balanced brackets or tags. For example if you want to match an opening html \fC<b>\fR and its closing \fC</b>\fR you can only use a regexp if you know that these tags are not nested; the html fragment, \fC<b>bold <b>bolder</b></b>\fR will not match as expected. If you know the maximum level of nesting it is possible to create a regexp that will match correctly, but for an unknown level of nesting regexps will fail..PPWe'll start by writing a regexp to match integers in the range 0 to 99. We will require at least one digit so we will start with \fB[0-9]{1,1}\fR which means match a digit exactly once. This regexp alone will match integers in the range 0 to 9. To match one or two digits we can increase the maximum number of occurrences so the regexp becomes \fB[0-9]{1,2}\fR meaning match a digit at least once and at most twice. However, this regexp as it stands will not match correctly. This regexp will match one or two digits \fIwithin\fR a string. To ensure that we match against the whole string we must use the anchor assertions. We need \fB^\fR (caret) which when it is the first character in the regexp means that the regexp must match from the beginning of the string. And we also need \fB$\fR (dollar) which when it is the last character in the regexp means that the regexp must match until the end of the string. So now our regexp is \fB^[0-9]{1,2}$\fR. Note that assertions, such as \fB^\fR and \fB$\fR, do not match any characters..PPIf you've seen regexps elsewhere they may have looked different from the ones above. This is because some sets of characters and some quantifiers are so common that they have special symbols to represent them. \fB[0-9]\fR can be replaced with the symbol \fB\\d\fR. The quantifier to match exactly one occurrence, \fB{1,1}\fR, can be replaced with the expression itself. This means that \fBx{1,1}\fR is exactly the same as \fBx\fR alone. So our 0 to 99 matcher could be written \fB^\\d{1,2}$\fR. Another way of writing it would be \fB^\\d\\d{0,1}$\fR, i.e. from the start of the string match a digit followed by zero or one digits. In practice most people would write it \fB^\\d\\d?$\fR. The \fB?\fR is a shorthand for the quantifier \fB{0,1}\fR, i.e. a minimum of no occurrences a maximum of one occurrence. This is used to make an expression optional. The regexp \fB^\\d\\d?$\fR means "from the beginning of the string match one digit followed by zero or one digits and then the end of the string"..PPOur second example is matching the words 'mail', 'letter' or 'correspondence' but without matching 'email', 'mailman', 'mailer', 'letterbox' etc. We'll start by just matching 'mail'. In full the regexp is, \fBm{1,1}a{1,1}i{1,1}l{1,1}\fR, but since each expression itself is automatically quantified by \fB{1,1}\fR we can simply write this as \fBmail\fR; an 'm' followed by an 'a' followed by an 'i' followed by an 'l'. The symbol '|' (bar) is used for \fIalternation\fR, so our regexp now becomes \fBmail|letter|correspondence\fR which means match 'mail' \fIor\fR 'letter' \fIor\fR 'correspondence'. Whilst this regexp will find the words we want it will also find words we don't want such as 'email'. We will start by putting our regexp in parentheses, \fB(mail|letter|correspondence)\fR. Parentheses have two effects, firstly they group expressions together and secondly they identify parts of the regexp that we wish to capture. Our regexp still matches any of the three words but now they are grouped together as a unit. This is useful for building up more complex regexps. It is also useful because it allows us to examine which of the words actually matched. We need to use another assertion, this time \fB\\b\fR "word boundary": \fB\\b(mail|letter|correspondence)\\b\fR. This regexp means "match a word boundary followed by the expression in parentheses followed by another word boundary". The \fB\\b\fR assertion matches at a \fIposition\fR in the regexp not a \fIcharacter\fR in the regexp. A word boundary is any non-word character such as a space a newline or the beginning or end of the string..PPFor our third example we want to replace ampersands with the HTML entity '&amp;'. The regexp to match is simple: \fB&\fR, i.e. match one ampersand. Unfortunately this will mess up our text if some of the ampersands have already been turned into HTML entities. So what we really want to say is replace an ampersand providing it is not followed by 'amp;'. For this we need the negative lookahead assertion and our regexp becomes: \fB&(?!amp;)\fR. The negative lookahead assertion is introduced with '(?!' and finishes at the ')'. It means that the text it contains, 'amp;' in our example, must \fInot\fR follow the expression that preceeds it..PPRegexps provide a rich language that can be used in a variety of ways. For example suppose we want to count all the occurrences of 'Eric' and 'Eirik' in a string. Two valid regexps to match these are \fB&#92;b(Eric|Eirik)&#92;b\fR and \fB&#92;bEi?ri[ck]&#92;b\fR. We need the word boundary '\\b' so we don't get 'Ericsson' etc. The second regexp actually matches more than we want, 'Eric', 'Erik', 'Eiric' and 'Eirik'..PPWe will implement some the examples above in the code examples section..SH "Characters and Abbreviations for Sets of Characters".IP.TP\fBc\fR Any character represents itself unless it has a special regexp meaning. Thus \fBc\fR matches the character \fIc\fR..IP.TP\fB&#92;c\fR A character that follows a backslash matches the character itself except where mentioned below. For example if you wished to match a literal caret at the beginning of a string you would write \fB&#92;^\fR..IP.TP\fB&#92;a\fR This matches the ASCII bell character (BEL, 0x07)..TP\fB&#92;f\fR This matches the ASCII form feed character (FF, 0x0C)..TP\fB&#92;n\fR This matches the ASCII line feed character (LF, 0x0A, Unix newline)..TP\fB&#92;r\fR This matches the ASCII carriage return character (CR, 0x0D)..TP\fB&#92;t\fR This matches the ASCII horizontal tab character (HT, 0x09)..TP\fB&#92;v\fR This matches the ASCII vertical tab character (VT, 0x0B)..TP\fB&#92;xhhhh\fR This matches the Unicode character corresponding to the hexadecimal number hhhh (between 0x0000 and 0xFFFF). &#92;0ooo (i.e., \\zero ooo) matches the ASCII/Latin-1 character corresponding to the octal number ooo (between 0 and 0377)..TP\fB. (dot)\fR This matches any character (including newline)..TP\fB&#92;d\fR This matches a digit (see QChar::isDigit())..TP\fB&#92;D\fR This matches a non-digit..TP\fB&#92;s\fR This matches a whitespace (see QChar::isSpace())..TP\fB&#92;S\fR This matches a non-whitespace..TP\fB&#92;w\fR This matches a word character (see QChar::isLetterOrNumber())..TP\fB&#92;W\fR This matches a non-word character..TP\fB&#92;n\fR The n-th backreference, e.g. &#92;1, &#92;2, etc..PP\fINote that the C++ compiler transforms backslashes in strings so to include a \fB&#92;\fR in a regexp you will need to enter it twice, i.e. \fB&#92;&#92;\fR.\fR.SH "Sets of Characters"Square brackets are used to match any character in the set of characters contained within the square brackets. All the character set abbreviations described above can be used within square brackets. Apart from the character set abbreviations and the following two exceptions no characters have special meanings in square brackets..IP.TP\fB^\fR The caret negates the character set if it occurs as the first character, i.e. immediately after the opening square bracket. For example, \fB[abc]\fR matches 'a' or 'b' or 'c', but \fB[^abc]\fR matches anything \fIexcept\fR 'a', 'b' and 'c'..IP.TP\fB-\fR The dash is used to indicate a range of characters, for example \fB[W-Z]\fR matches 'W' or 'X' or 'Y' or 'Z'..IP.PPUsing the predefined character set abbreviations is more portable than using character ranges across platforms and languages. For example, \fB[0-9]\fR matches a digit in Western alphabets but \fB\\d\fR matches a digit in \fIany\fR alphabet..PPNote that in most regexp literature sets of characters are called" character classes"..SH "Quantifiers"By default an expression is automatically quantified by \fB{1,1}\fR, i.e. it should occur exactly once. In the following list \fB\fIE\fR\fR stands for any expression. An expression is a character or an abbreviation for a set of characters or a set of characters in square brackets or any parenthesised expression..IP.TP\fB\fIE\fR\fR Matches zero or one occurrence of \fIE\fR. This quantifier means "the previous expression is optional" since it will match whether or not the expression occurs in the string. It is the same as \fB\fIE\fR{0,1}\fR. For example \fBdents?\fR will match 'dent' and 'dents'..IP.TP\fB\fIE\fR+\fR Matches one or more occurrences of \fIE\fR. This is the same as \fB\fIE\fR{1,MAXINT}\fR. For example, \fB0+\fR will match '0', '00', '000', etc..IP.TP\fB\fIE\fR*\fR Matches zero or more occurrences of \fIE\fR. This is the same as \fB\fIE\fR{0,MAXINT}\fR. The \fB*\fR quantifier is often used by a mistake. Since it matches \fIzero\fR or more occurrences it will match no occurrences at all. For example if we want to match strings that end in whitespace and use the regexp \fB\\s*$\fR we would get a match on every string. This is because we have said find zero or more whitespace followed by the end of string, so even strings that don't end in whitespace will match. The regexp we want in this case is \fB\\s+$\fR to match strings that have at least one whitespace at the end..IP.TP\fB\fIE\fR{n}\fR Matches exactly \fIn\fR occurrences of the expression. This is the same as repeating the expression \fIn\fR times. For example, \fBx{5}\fR is the same as \fBxxxxx\fR. It is also the same as \fB\fIE\fR{n,n}\fR, e.g. \fBx{5,5}\fR..IP.TP\fB\fIE\fR{n,}\fR Matches at least \fIn\fR occurrences of the expression. This is the same as \fB\fIE\fR{n,MAXINT}\fR..IP.TP\fB\fIE\fR{,m}\fR Matches at most \fIm\fR occurrences of the expression. This is the same as \fB\fIE\fR{0,m}\fR..IP.TP\fB\fIE\fR{n,m}\fR Matches at least \fIn\fR occurrences of the expression and at most \fIm\fR occurrences of the expression..IP.PP(MAXINT is implementation dependent but will not be smaller than 1024.).PPIf we wish to apply a quantifier to more than just the preceding character we can use parentheses to group characters together in an expression. For example, \fBtag+\fR matches a 't' followed by an 'a' followed by at least one 'g', whereas \fB(tag)+\fR matches at least one occurrence of 'tag'..PPNote that quantifiers are "greedy". They will match as much text as they can. For example, \fB0+\fR will match as many zeros as it can from the first zero it finds, e.g. '2.<u>000</u>5'. Quantifiers can be made non-greedy, see setMinimal()..SH "Capturing Text"Parentheses allow us to group elements together so that we can quantify and capture them. For example if we have the expression \fBmail|letter|correspondence\fR that matches a string we know that \fIone\fR of the words matched but not which one. Using parentheses allows us to "capture" whatever is matched within their bounds, so if we used \fB(mail|letter|correspondence)\fR and matched this regexp against the string "I sent you some email" we can use the cap() or capturedTexts() functions to extract the matched characters, in this case 'mail'..PPWe can use captured text within the regexp itself. To refer to the captured text we use \fIbackreferences\fR which are indexed from 1, the same as for cap(). For example we could search for duplicate words in a string using \fB\\b(\\w+)\\W+&#92;1\\b\fR which means match a word boundary followed by one or more word characters followed by one or more non-word characters followed by the same text as the first parenthesised expression followed by a word boundary..PPIf we want to use parentheses purely for grouping and not for capturing we can use the non-capturing syntax, e.g. \fB(?:green|blue)\fR. Non-capturing parentheses begin '(?:' and end ')'. In this example we match either 'green' or 'blue' but we do not capture the match so we only know whether or not we matched but not which color we actually found. Using non-capturing parentheses is more efficient than using capturing parentheses since the regexp engine has to do less book-keeping..PPBoth capturing and non-capturing parentheses may be nested..SH "Assertions"Assertions make some statement about the text at the point where they occur in the regexp but they do not match any characters. In the following list \fB\fIE\fR\fR stands for any expression..TP\fB^\fR The caret signifies the beginning of the string. If you wish to match a literal \fC^\fR you must escape it by writing \fC&#92;^\fR. For example, \fB^#include\fR will only match strings which \fIbegin\fR with the characters '#include'. (When the caret is the first character of a character set it has a special meaning, see Sets of Characters.).IP.TP\fB$\fR The dollar signifies the end of the string. For example \fB\\d\\s*$\fR will match strings which end with a digit optionally followed by whitespace. If you wish to match a literal \fC$\fR you must escape it by writing \fC&#92;$\fR..IP.TP\fB&#92;b\fR A word boundary. For example the regexp \fB&#92;bOK\fR&#92;b</b> means match immediately after a word boundary (e.g. start of string or whitespace) the letter 'O' then the letter 'K' immediately before another word boundary (e.g. end of string or whitespace). But note that the assertion does not actually match any whitespace so if we write \fB(&#92;bOK\fR&#92;b)</b> and we have a match it will only contain 'OK' even if the string is "Its <u>OK</u> now"..IP.TP\fB&#92;B\fR A non-word boundary. This assertion is true wherever \fB&#92;b\fR is false. For example if we searched for \fB&#92;Bon\fR&#92;B</b> in "Left on" the match would fail (space and end of string aren't non-word boundaries), but it would match in "t<u>on</u>ne"..IP.TP\fB(?=\fIE\fR)\fR Positive lookahead. This assertion is true if the expression matches at this point in the regexp. This assertion does not match any characters. For example, \fB^#define\\s+(\\w+)(?=MAX)\fR will match strings which begin with '#define' followed by at least one whitespace followed by at least one word character followed by 'MAX'. The first set of parentheses will capture the word character(s) matched. This regexp will not match '#define DEBUG' but will match '#define <u>INT</u>MAX 32767'..IP.TP\fB(?!\fIE\fR)\fR Negative lookahead. This assertion is true if the expression does not match at this point in the regexp. This assertion does not match any characters. For example, \fB^#define\\s+(\\w+)\\s*$\fR will match strings which begin with '#define' followed by at least one whitespace followed by at least one word character optionally followed by whitespace. This regexp will match define's that exist but have no value, i.e. it will not match '#define INTMAX 32767' but it will match '#define <u>DEBUG</u>'.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -