📄 qregexp.3qt
字号:
.IP.SH "Wildcard Matching (globbing)"Most command shells such as \fIbash\fR or \fIcmd\fR support "file globbing", the ability to identify a group of files by using wildcards. The setWildcard() function is used to switch between regexp and wildcard mode. Wildcard matching is much simpler than full regexps and has only four features:.IP.TP\fBc\fR Any character represents itself apart from those mentioned below. Thus \fBc\fR matches the character \fIc\fR..IP.TP\fB?\fR This matches any single character. It is the same as \fB.\fR in full regexps..IP.TP\fB*\fR This matches zero or more of any characters. It is the same as \fB.*\fR in full regexps..IP.TP\fB[...]\fR Sets of characters can be represented in square brackets, similar to full regexps. Within the character class, like outside, backslash has no special meaning..IP.PPFor example if we are in wildcard mode and have strings which contain filenames we could identify HTML files with \fB*.html\fR. This will match zero or more characters followed by a dot followed by 'h', 't', 'm' and 'l'..SH "Notes for Perl Users"Most of the character class abbreviations supported by Perl are supported by QRegExp, see characters and abbreviations for sets of characters..PPIn QRegExp, apart from within character classes, \fC^\fR always signifies the start of the string, so carets must always be escaped unless used for that purpose. In Perl the meaning of caret varies automagically depending on where it occurs so escaping it is rarely necessary. The same applies to \fC$\fR which in QRegExp always signifies the end of the string..PPQRegExp's quantifiers are the same as Perl's greedy quantifiers. Non-greedy matching cannot be applied to individual quantifiers, but can be applied to all the quantifiers in the pattern. For example, to match the Perl regexp \fBro+?m\fR requires:.PP.nf.br QRegExp rx( "ro+m" );.br rx.setMinimal( TRUE );.br.fi.PPThe equivalent of Perl's \fC/i\fR option is setCaseSensitive(FALSE)..PPPerl's \fC/g\fR option can be emulated using a loop..PPIn QRegExp \fB.\fR matches any character, therefore all QRegExp regexps have the equivalent of Perl's \fC/s\fR option. QRegExp does not have an equivalent to Perl's \fC/m\fR option, but this can be emulated in various ways for example by splitting the input into lines or by looping with a regexp that searches for newlines..PPBecause QRegExp is string oriented there are no \\A, \\Z or \\z assertions. The \\G assertion is not supported but can be emulated in a loop..PPPerl's $& is cap(0) or capturedTexts()[0]. There are no QRegExp equivalents for $`, $' or $+. Perl's capturing variables, $1, $2, ... correspond to cap(1) or capturedTexts()[1], cap(2) or capturedTexts()[2], etc..PPTo substitute a pattern use QString::replace()..PPPerl's extended \fC/x\fR syntax is not supported, nor are regexp comments (?#comment) or directives, e.g. (?i)..PPBoth zero-width positive and zero-width negative lookahead assertions (?=pattern) and (?!pattern) are supported with the same syntax as Perl. Perl's lookbehind assertions, "independent" subexpressions and conditional expressions are not supported..PPNon-capturing parentheses are also supported, with the same (?:pattern) syntax..PPSee QStringList::split() and QStringList::join() for equivalents to Perl's split and join functions..PPNote: because C++ transforms \'s they must be written \fItwice\fR in code, e.g. \fB\b\fR must be written \fB\\b\fR..SH "Code Examples".nf.br QRegExp rx( "^\\\\d\\\\d?$" ); // match integers 0 to 99.br rx.search( "123" ); // returns -1 (no match).br rx.search( "-6" ); // returns -1 (no match).br rx.search( "6" ); // returns 0 (matched as position 0).br.fi.PPThe third string matches '<u>6</u>'. This is a simple validation regexp for integers in the range 0 to 99..PP.nf.br QRegExp rx( "^\\\\S+$" ); // match strings without whitespace.br rx.search( "Hello world" ); // returns -1 (no match).br rx.search( "This_is-OK" ); // returns 0 (matched at position 0).br.fi.PPThe second string matches '<u>This_is-OK</u>'. We've used the character set abbreviation '\\S' (non-whitespace) and the anchors to match strings which contain no whitespace..PPIn the following example we match strings containing 'mail' or 'letter' or 'correspondence' but only match whole words i.e. not 'email'.PP.nf.br QRegExp rx( "\\\\b(mail|letter|correspondence)\\\\b" );.br rx.search( "I sent you an email" ); // returns -1 (no match).br rx.search( "Please write the letter" ); // returns 17.br.fi.PPThe second string matches "Please write the <u>letter</u>". The word 'letter' is also captured (because of the parentheses). We can see what text we've captured like this:.PP.nf.br QString captured = rx.cap( 1 ); // captured contains "letter".br.fi.PPThis will capture the text from the first set of capturing parentheses (counting capturing left parentheses from left to right). The parentheses are counted from 1 since cap( 0 ) is the whole matched regexp (equivalent to '&' in most regexp engines)..PP.nf.br QRegExp rx( "&(?!amp;)" ); // match ampersands but not &.br QString line1 = "This & that";.br line1.replace( rx, "&" );.br // line1 == "This & that".br QString line2 = "His & hers & theirs";.br line2.replace( rx, "&" );.br // line2 == "His & hers & theirs".br.fi.PPHere we've passed the QRegExp to QString's replace() function to replace the matched text with new text..PP.nf.br QString str = "One Eric another Eirik, and an Ericsson.".br " How many Eiriks, Eric?";.br QRegExp rx( "\\\\b(Eric|Eirik)\\\\b" ); // match Eric or Eirik.br int pos = 0; // where we are in the string.br int count = 0; // how many Eric and Eirik's we've counted.br while ( pos >= 0 ) {.br pos = rx.search( str, pos );.br if ( pos >= 0 ) {.br pos++; // move along in str.br count++; // count our Eric or Eirik.br }.br }.br.fi.PPWe've used the search() function to repeatedly match the regexp in the string. Note that instead of moving forward by one character at a time \fCpos++\fR we could have written \fCpos += rx.matchedLength()\fR to skip over the already matched string. The count will equal 3, matching 'One <u>Eric</u> another <u>Eirik</u>, and an Ericsson. How many Eiriks, <u>Eric</u>?'; it doesn't match 'Ericsson' or 'Eiriks' because they are not bounded by non-word boundaries..PPOne common use of regexps is to split lines of delimited data into their component fields..PP.nf.br str = "Trolltech AS\\twww.trolltech.com\\tNorway";.br QString company, web, country;.br rx.setPattern( "^([^\\t]+)\\t([^\\t]+)\\t([^\\t]+)$" );.br if ( rx.search( str ) != -1 ) {.br company = rx.cap( 1 );.br web = rx.cap( 2 );.br country = rx.cap( 3 );.br }.br.fi.PPIn this example our input lines have the format company name, web address and country. Unfortunately the regexp is rather long and not very versatile -- the code will break if we add any more fields. A simpler and better solution is to look for the separator, '\\t' in this case, and take the surrounding text. The QStringList split() function can take a separator string or regexp as an argument and split a string accordingly..PP.nf.br QStringList field = QStringList::split( "\\t", str );.br.fi.PPHere field[0] is the company, field[1] the web address and so on..PPTo imitate the matching of a shell we can use wildcard mode..PP.nf.br QRegExp rx( "*.html" ); // invalid regexp: * doesn't quantify anything.br rx.setWildcard( TRUE ); // now it's a valid wildcard regexp.br rx.search( "index.html" ); // returns 0 (matched at position 0).br rx.search( "default.htm" ); // returns -1 (no match).br rx.search( "readme.txt" ); // returns -1 (no match).br.fi.PPWildcard matching can be convenient because of its simplicity, but any wildcard regexp can be defined using full regexps, e.g. \fB.*\.html$\fR. Notice that we can't match both \fC.html\fR and \fC.htm\fR files with a wildcard unless we use \fB*.htm*\fR which will also match 'test.html.bak'. A full regexp gives us the precision we need, \fB.*\.html?$\fR..PPQRegExp can match case insensitively using setCaseSensitive(), and can use non-greedy matching, see setMinimal(). By default QRegExp uses full regexps but this can be changed with setWildcard(). Searching can be forward with search() or backward with searchRev(). Captured text can be accessed using capturedTexts() which returns a string list of all captured strings, or using cap() which returns the captured string for the given index. The pos() function takes a match index and returns the position in the string where the match was made (or -1 if there was no match)..PPSee also QRegExpValidator, QString, QStringList, Miscellaneous Classes, Implicitly and Explicitly Shared Classes and Non-GUI Classes..PP.SH MEMBER FUNCTION DOCUMENTATION.SH "QRegExp::QRegExp ()"Constructs an empty regexp..PPSee also isValid()..SH "QRegExp::QRegExp ( const QString & pattern, bool caseSensitive = TRUE, bool wildcard = FALSE )"Constructs a regular expression object for the given \fIpattern\fR string. The pattern must be given using wildcard notation if \fIwildcard\fR is TRUE (default is FALSE). The pattern is case sensitive, unless \fIcaseSensitive\fR is FALSE. Matching is greedy (maximal), but can be changed by calling setMinimal()..PPSee also setPattern(), setCaseSensitive(), setWildcard() and setMinimal()..SH "QRegExp::QRegExp ( const QRegExp & rx )"Constructs a regular expression as a copy of \fIrx\fR..PPSee also operator=()..SH "QRegExp::~QRegExp ()"Destroys the regular expression and cleans up its internal data..SH "QString QRegExp::cap ( int nth = 0 )"Returns the text captured by the \fInth\fR subexpression. The entire match has index 0 and the parenthesized subexpressions have indices starting from 1 (excluding non-capturing parentheses)..PP.nf.br QRegExp rxlen( "(\\\\d+)(?:\\\\s*)(cm|inch)" );.br int pos = rxlen.search( "Length: 189cm" );.br if ( pos > -1 ) {.br QString value = rxlen.cap( 1 ); // "189".br QString unit = rxlen.cap( 2 ); // "cm".br // ....br }.br.fi.PPThe order of elements matched by cap() is as follows. The first element, cap(0), is the entire matching string. Each subsequent element corresponds to the next capturing open left parentheses. Thus cap(1) is the text of the first capturing parentheses, cap(2) is the text of the second, and so on..PPSome patterns may lead to a number of matches which cannot be determined in advance, for example:.PP.nf.br QRegExp rx( "(\\\\d+)" );.br str = "Offsets: 12 14 99 231 7";.br QStringList list;.br pos = 0;.br while ( pos >= 0 ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -