#include </home/clem/local/src/opie/qmake/include/qregexp.h>
Collaboration diagram for QRegExp:

Public Types | |
| enum | CaretMode { CaretAtZero, CaretAtOffset, CaretWontMatch } |
Public Member Functions | |
| QRegExp () | |
| QRegExp (const QString &pattern, bool caseSensitive=TRUE, bool wildcard=FALSE) | |
| QRegExp (const QRegExp &rx) | |
| ~QRegExp () | |
| QRegExp & | operator= (const QRegExp &rx) |
| bool | operator== (const QRegExp &rx) const |
| bool | operator!= (const QRegExp &rx) const |
| bool | isEmpty () const |
| bool | isValid () const |
| QString | pattern () const |
| void | setPattern (const QString &pattern) |
| bool | caseSensitive () const |
| void | setCaseSensitive (bool sensitive) |
| bool | wildcard () const |
| void | setWildcard (bool wildcard) |
| bool | minimal () const |
| void | setMinimal (bool minimal) |
| bool | exactMatch (const QString &str) const |
| int | match (const QString &str, int index=0, int *len=0, bool indexIsStart=TRUE) const |
| int | search (const QString &str, int offset=0) const |
| int | search (const QString &str, int offset, CaretMode caretMode) const |
| int | searchRev (const QString &str, int offset=-1) const |
| int | searchRev (const QString &str, int offset, CaretMode caretMode) const |
| int | matchedLength () const |
| int | numCaptures () const |
| QStringList | capturedTexts () |
| QString | cap (int nth=0) |
| int | pos (int nth=0) |
| QString | errorString () |
Static Public Member Functions | |
| static QString | escape (const QString &str) |
Private Member Functions | |
| void | compile (bool caseSensitive) |
Static Private Member Functions | |
| static int | caretIndex (int offset, CaretMode caretMode) |
Private Attributes | |
| QRegExpEngine * | eng |
| QRegExpPrivate * | priv |
regular expression
Regular expressions, or "regexps", provide a way to find patterns within text. This is useful in many contexts, for example:
Validation A regexp can be used to check whether a piece of text meets some criteria, e.g. is an integer or contains no whitespace. Searching 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." Search and Replace A regexp can be used to replace a pattern with a piece of text, for example replace all occurrences of '&' with '&' except where the '&' is already followed by 'amp;'. String Splitting A regexp can be used to identify where a string should be split into its component fields, e.g. splitting tab-delimited strings.
We present a very brief introduction to regexps, a description of Qt's regexp language, some code examples, and finally the function documentation itself. QRegExp is modeled on Perl's regexp language, and also fully supports Unicode. QRegExp can also be used in the weaker 'wildcard' (globbing) mode which works in a similar way to command shells. A good text on regexps is {Mastering Regular Expressions: Powerful Techniques for Perl and Other Tools} by Jeffrey E. Friedl, ISBN 1565922573.
Experienced regexp users may prefer to skip the introduction and go directly to the relevant information.
Regexps are built up from expressions, quantifiers, and assertions. The simplest form of expression is simply a character, e.g. <b>x</b> or <b>5</b>. An expression can also be a set of characters. For example, <b>[ABCD]</b>, will match an <b>A</b> or a <b>B</b> or a <b>C</b> or a <b>D</b>. As a shorthand we could write this as <b>[A-D]</b>. If we want to match any of the captital letters in the English alphabet we can write <b>[A-Z]</b>. A quantifier tells the regexp engine how many occurrences of the expression we want, e.g. <b>x{1,1}</b> means match an <b>x</b> which occurs at least once and at most once. We'll look at assertions and more complex expressions later. Note that in general regexps cannot be used to check for balanced brackets or tags. For example if you want to match an opening html \c <b> and its closing \c </b> you can only use a regexp if you know that these tags are not nested; the html fragment, \c{<b>bold <b>bolder</b></b>} 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. We'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 <b>[0-9]{1,1}</b> 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 <b>[0-9]{1,2}</b> 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 \e within a string. To ensure that we match against the whole string we must use the anchor assertions. We need <b>^</b> (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 <b>$</b> (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 <b>^[0-9]{1,2}$</b>. Note that assertions, such as <b>^</b> and <b>$</b>, do not match any characters. If 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. <b>[0-9]</b> can be replaced with the symbol <b>\d</b>. The quantifier to match exactly one occurrence, <b>{1,1}</b>, can be replaced with the expression itself. This means that <b>x{1,1}</b> is exactly the same as <b>x</b> alone. So our 0 to 99 matcher could be written <b>^\d{1,2}$</b>. Another way of writing it would be <b>^\d\d{0,1}$</b>, i.e. from the start of the string match a digit followed by zero or one digits. In practice most people would write it <b>^\d\d?$</b>. The <b>?</b> is a shorthand for the quantifier <b>{0,1}</b>, i.e. a minimum of no occurrences a maximum of one occurrence. This is used to make an expression optional. The regexp <b>^\d\d?$</b> means "from the beginning of the string match one digit followed by zero or one digits and then the end of the string". Our 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, <b>m{1,1}a{1,1}i{1,1}l{1,1}</b>, but since each expression itself is automatically quantified by <b>{1,1}</b> we can simply write this as <b>mail</b>; an 'm' followed by an 'a' followed by an 'i' followed by an 'l'. The symbol '|' (bar) is used for \e alternation, so our regexp now becomes <b>mail|letter|correspondence</b> which means match 'mail' \e or 'letter' \e or '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, <b>(mail|letter|correspondence)</b>. Parentheses have two effects, firstly they group expressions together and secondly they identify parts of the regexp that we wish to \link #capturing-text capture \endlink. 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 <b>\b</b> "word boundary": <b>\b(mail|letter|correspondence)\b</b>. This regexp means "match a word boundary followed by the expression in parentheses followed by another word boundary". The <b>\b</b> assertion matches at a \e position in the regexp not a \e character 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. For our third example we want to replace ampersands with the HTML entity '\&'. The regexp to match is simple: <b>\&</b>, 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: <b>\&(?!amp;)</b>. The negative lookahead assertion is introduced with '(?!' and finishes at the ')'. It means that the text it contains, 'amp;' in our example, must \e not follow the expression that preceeds it. Regexps 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 <b>\\b(Eric|Eirik)\\b</b> and <b>\\bEi?ri[ck]\\b</b>. 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'. We will implement some the examples above in the \link #code-examples code examples \endlink section. \target characters-and-abbreviations-for-sets-of-chDefinition at line 49 of file qregexp.h.
|
|
The CaretMode enum defines the different meanings of the caret (^) in a regular expression. The possible values are: CaretAtZero The caret corresponds to index 0 in the searched string. CaretAtOffset The caret corresponds to the start offset of the search. CaretWontMatch The caret never matches. |
|
|
Constructs an empty regexp.
Definition at line 3265 of file qregexp.cpp. References compile(), FALSE, QRegExpPrivate::min, priv, and QRegExpPrivate::wc. |
|
||||||||||||||||
|
Constructs a regular expression object for the given pattern string. The pattern must be given using wildcard notation if wildcard is TRUE (default is FALSE). The pattern is case sensitive, unless caseSensitive is FALSE. Matching is greedy (maximal), but can be changed by calling setMinimal().
Definition at line 3285 of file qregexp.cpp. References compile(), FALSE, QRegExpPrivate::min, QRegExpPrivate::pattern, priv, and QRegExpPrivate::wc. |
|
|
Constructs a regular expression as a copy of rx.
Definition at line 3302 of file qregexp.cpp. References operator=(), and priv. |
|
|
Destroys the regular expression and cleans up its internal data. Definition at line 3312 of file qregexp.cpp. References derefEngine(), priv, and QRegExpPrivate::rxpattern. |
|
|
Returns the text captured by the nth subexpression. The entire match has index 0 and the parenthesized subexpressions have indices starting from 1 (excluding non-capturing parentheses).
QRegExp rxlen( "(\\d+)(?:\\s*)(cm|inch)" ); int pos = rxlen.search( "Length: 189cm" ); if ( pos > -1 ) { QString value = rxlen.cap( 1 ); // "189" QString unit = rxlen.cap( 2 ); // "cm" // ... } The 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. cap_in_a_loop Some patterns may lead to a number of matches which cannot be determined in advance, for example:
QRegExp rx( "(\\d+)" ); str = "Offsets: 12 14 99 231 7"; QStringList list; pos = 0; while ( pos >= 0 ) { pos = rx.search( str, pos ); if ( pos > -1 ) { list += rx.cap( 1 ); pos += rx.matchedLength(); } } // list contains "12", "14", "99", "231", "7"
Definition at line 3829 of file qregexp.cpp. References QRegExpPrivate::captured, capturedTexts(), QString::null, and priv. Referenced by Win32MakefileGenerator::findHighestVersion(), UnixMakefileGenerator::findLibraries(), ProjectBuilderMakefileGenerator::fixEnvs(), linkLib(), ProjectBuilderMakefileGenerator::pbuilderVersion(), MetrowerksMakefileGenerator::processPrlFiles(), proFileTagMap(), QString::replace(), and MingwMakefileGenerator::writeSubDirs(). |
|
|
Returns a list of the captured text strings. The first string in the list is the entire matched string. Each subsequent list element contains a string that matched a (capturing) subexpression of the regexp. For example: QRegExp rx( "(\\d+)(\\s*)(cm|inch(es)?)" ); int pos = rx.search( "Length: 36 inches" ); QStringList list = rx.capturedTexts(); // list is now ( "36 inches", "36", " ", "inches", "es" ) The above example also captures elements that may be present but which we have no interest in. This problem can be solved by using non-capturing parentheses:
QRegExp rx( "(\\d+)(?:\\s*)(cm|inch(?:es)?)" ); int pos = rx.search( "Length: 36 inches" ); QStringList list = rx.capturedTexts(); // list is now ( "36 inches", "36", "inches" ) Note that if you want to iterate over the list, you should iterate over a copy, e.g. QStringList list = rx.capturedTexts(); QStringList::Iterator it = list.begin(); while( it != list.end() ) { myProcessing( *it ); ++it; }
Some regexps can match an indeterminate number of times. For example if the input string is "Offsets: 12 14 99 231 7" and the regexp, The order of elements in the string list is as follows. The first element is the entire matching string. Each subsequent element corresponds to the next capturing open left parentheses. Thus capturedTexts()[1] is the text of the first capturing parentheses, capturedTexts()[2] is the text of the second and so on (corresponding to $1, $2, etc., in some other regexp languages).
Definition at line 3770 of file qregexp.cpp. References QValueList< T >::append(), QRegExpPrivate::captured, QRegExpPrivate::capturedCache, QString::fromLatin1(), QValueList< T >::isEmpty(), QString::mid(), QString::null, priv, and QRegExpPrivate::t. Referenced by cap(). |
|
||||||||||||
|
Definition at line 3932 of file qregexp.cpp. References CaretAtOffset, and CaretAtZero. Referenced by search(), and searchRev(). |
|
|
Returns TRUE if case sensitivity is enabled; otherwise returns FALSE. The default is TRUE.
Definition at line 3439 of file qregexp.cpp. References QRegExpEngine::caseSensitive(). Referenced by setPattern(), and setWildcard(). |
|
|
Definition at line 3913 of file qregexp.cpp. References QRegExpPrivate::captured, QRegExpPrivate::capturedCache, QValueList< T >::clear(), derefEngine(), QString::fromLatin1(), QString::isNull(), newEngine(), QRegExpEngine::numCaptures(), QRegExpPrivate::pattern, priv, QRegExpPrivate::rxpattern, QRegExpPrivate::t, QRegExpPrivate::wc, and wc2rx(). Referenced by QRegExp(), setCaseSensitive(), setPattern(), and setWildcard(). |
|
|
Returns a text string that explains why a regexp pattern is invalid the case being; otherwise returns "no error occurred".
Definition at line 3871 of file qregexp.cpp. References QRegExpEngine::errorString(), isValid(), and RXERR_OK. |
|
|
Returns the string str with every regexp special character escaped with a backslash. The special characters are $, (, ), *, +, ., ?, [, \, ], ^, {, | and }. Example: s1 = QRegExp::escape( "bingo" ); // s1 == "bingo" s2 = QRegExp::escape( "f(x)" ); // s2 == "f\\(x\\)" This function is useful to construct regexp patterns dynamically:
QRegExp rx( "(" + QRegExp::escape(name) + "|" + QRegExp::escape(alias) + ")" ); Definition at line 3899 of file qregexp.cpp. References QString::insert(), and QString::length(). Referenced by striphtml::findanchor(). |
|
|
Returns TRUE if str is matched exactly by this regular expression; otherwise returns FALSE. You can determine how much of the string was matched by calling matchedLength(). For a given regexp string, R, exactMatch("R") is the equivalent of search("^R$") since exactMatch() effectively encloses the regexp in the start of string and end of string anchors, except that it sets matchedLength() differently.
For example, if the regular expression is blue, then exactMatch() returns TRUE only for input Although const, this function sets matchedLength(), capturedTexts() and pos().
Definition at line 3545 of file qregexp.cpp. References QRegExpPrivate::captured, QRegExpPrivate::capturedCache, QValueList< T >::clear(), FALSE, QString::length(), QRegExpEngine::match(), QRegExpEngine::matchedLength(), QRegExpPrivate::min, priv, and QRegExpPrivate::t. Referenced by QMakeProject::doProjectTest(), Win32MakefileGenerator::findHighestVersion(), UnixMakefileGenerator::findLibraries(), QMakeProject::isActiveConfig(), linkLib(), QDir::match(), ProjectBuilderMakefileGenerator::pbuilderVersion(), MetrowerksMakefileGenerator::processPrlFiles(), and UnixMakefileGenerator::writeExtraVariables(). |
|
|
Returns TRUE if the pattern string is empty; otherwise returns FALSE. If you call exactMatch() with an empty pattern on an empty string it will return TRUE; otherwise it returns FALSE since it operates over the whole string. If you call search() with an empty pattern on any string it will return the start offset (0 by default) because the empty pattern matches the 'emptiness' at the start of the string. In this case the length of the match returned by matchedLength() will be 0. See QString::isEmpty(). Definition at line 3384 of file qregexp.cpp. References QString::isEmpty(), QRegExpPrivate::pattern, and priv. Referenced by LauncherIconView::addItem(), SearchGroup::doSearch(), and SearchGroup::expand(). |
|
|
Returns TRUE if the regular expression is valid; otherwise returns FALSE. An invalid regular expression never matches. The pattern [a-z is an example of an invalid pattern, since it lacks a closing square bracket. Note that the validity of a regexp may also depend on the setting of the wildcard flag, for example *.html is a valid wildcard regexp but an invalid full regexp.
Definition at line 3402 of file qregexp.cpp. References QRegExpEngine::isValid(). Referenced by errorString(), and SearchDialog::slotOk(). |
|
||||||||||||||||||||
|
|
Returns the length of the last matched string, or -1 if there was no match.
Definition at line 3699 of file qregexp.cpp. References QRegExpPrivate::captured, and priv. Referenced by ProjectBuilderMakefileGenerator::fixEnvs(), fixEnvVariables(), match(), proFileTagMap(), QString::replace(), QString::section(), QStringList::split(), and QString::sprintf(). |
|
|
Returns TRUE if minimal (non-greedy) matching is enabled; otherwise returns FALSE.
Definition at line 3498 of file qregexp.cpp. References QRegExpPrivate::min, and priv. |
|
|
Returns the number of captures contained in the regular expression. Definition at line 3708 of file qregexp.cpp. References QRegExpEngine::numCaptures(). Referenced by QString::replace(). |
|
|
Returns TRUE if this regular expression is not equal to rx; otherwise returns FALSE.
Definition at line 62 of file qregexp.h. References operator==(). |
|
|
Copies the regular expression rx and returns a reference to the copy. The case sensitivity, wildcard and minimal matching options are also copied. Definition at line 3323 of file qregexp.cpp. References QRegExpPrivate::captured, QRegExpPrivate::capturedCache, derefEngine(), eng, QRegExpPrivate::min, QRegExpPrivate::pattern, priv, QShared::ref(), QRegExpPrivate::rxpattern, QRegExpPrivate::t, and QRegExpPrivate::wc. Referenced by QRegExp(). |
|
|
Returns TRUE if this regular expression is equal to rx; otherwise returns FALSE. Two QRegExp objects are equal if they have the same pattern strings and the same settings for case sensitivity, wildcard and minimal matching. Definition at line 3350 of file qregexp.cpp. References QRegExpEngine::caseSensitive(), eng, QRegExpPrivate::min, QRegExpPrivate::pattern, priv, and QRegExpPrivate::wc. |
|
|
Returns the pattern string of the regular expression. The pattern has either regular expression syntax or wildcard syntax, depending on wildcard().
Definition at line 3414 of file qregexp.cpp. References QRegExpPrivate::pattern, and priv. Referenced by SearchGroup::expand(), Opie::OPimTodoAccessBackendSQL::matchRegexp(), Opie::OPimContactAccessBackend_SQL::matchRegexp(), SearchGroup::realSearch(), and SearchGroup::setSearch(). |
|
|
Returns the position of the nth captured text in the searched string. If nth is 0 (the default), pos() returns the position of the whole match. Example: QRegExp rx( "/([a-z]+)/([a-z]+)" ); rx.search( "Output /dev/null" ); // returns 7 (position of /dev/null) rx.pos( 0 ); // returns 7 (position of /dev/null) rx.pos( 1 ); // returns 8 (position of dev) rx.pos( 2 ); // returns 12 (position of null) For zero-length matches, pos() always returns -1. (For example, if cap(4) would return an empty string, pos(4) returns -1.) This is due to an implementation tradeoff.
Definition at line 3857 of file qregexp.cpp. References QRegExpPrivate::captured, and priv. |
|
||||||||||||||||
|
Attempts to find a match in str from position offset (0 by default). If offset is -1, the search starts at the last character; if -2, at the next to last character; etc. Returns the position of the first match, or -1 if there was no match. The caretMode parameter can be used to instruct whether ^ should match at index 0 or at offset. You might prefer to use QString::find(), QString::contains() or even QStringList::grep(). To replace matches use QString::replace(). Example: QString str = "offsets: 1.23 .50 71.00 6.00"; QRegExp rx( "\\d*\\.\\d+" ); // primitive floating point matching int count = 0; int pos = 0; while ( (pos = rx.search(str, pos)) != -1 ) { count++; pos += rx.matchedLength(); } // pos will be 9, 14, 18 and finally 24; count will end up as 4 Although const, this function sets matchedLength(), capturedTexts() and pos().
Definition at line 3629 of file qregexp.cpp. References QRegExpPrivate::captured, QRegExpPrivate::capturedCache, caretIndex(), QValueList< T >::clear(), FALSE, QString::length(), QRegExpEngine::match(), QRegExpPrivate::min, priv, and QRegExpPrivate::t. |
|
||||||||||||
|
Definition at line 3590 of file qregexp.cpp. References CaretAtZero. Referenced by QMakeProject::doVariableReplace(), QString::find(), ProjectBuilderMakefileGenerator::fixEnvs(), fixEnvVariables(), MakefileGenerator::generateDependencies(), match(), proFileTagMap(), QString::replace(), QString::section(), QStringList::split(), QString::sprintf(), and MingwMakefileGenerator::writeSubDirs(). |
|
||||||||||||||||
|
Attempts to find a match backwards in str from position offset. If offset is -1 (the default), the search starts at the last character; if -2, at the next to last character; etc. Returns the position of the first match, or -1 if there was no match. The caretMode parameter can be used to instruct whether ^ should match at index 0 or at offset. Although const, this function sets matchedLength(), capturedTexts() and pos().
Definition at line 3668 of file qregexp.cpp. References QRegExpPrivate::captured, QRegExpPrivate::capturedCache, caretIndex(), QValueList< T >::clear(), QString::length(), QRegExpEngine::match(), QRegExpPrivate::min, priv, and QRegExpPrivate::t. |
|
||||||||||||
|
Definition at line 3643 of file qregexp.cpp. References CaretAtZero. |
|
|
Sets case sensitive matching to sensitive.
If sensitive is TRUE, \.txt$ matches
Definition at line 3452 of file qregexp.cpp. References QRegExpEngine::caseSensitive(), and compile(). Referenced by DingWidget::parseInfo(), QString::section(), SConfig::setPattern(), DateBook::slotDoFind(), and AbView::slotDoFind(). |
|
|
Enables or disables minimal matching. If minimal is FALSE, matching is greedy (maximal) which is the default. For example, suppose we have the input string "We must be <b>bold</b>, very <b>bold</b>!" and the pattern <b>.*</b>. With the default greedy (maximal) matching, the match is "We must be <u><b>bold</b>, very <b>bold</b></u>!". But with minimal (non-greedy) matching the first match is: "We must be <u><b>bold</b></u>, very <b>bold</b>!" and the second match is "We must be <b>bold</b>, very <u><b>bold</b></u>!". In practice we might use the pattern <b>[^<]+</b> instead, although this will still fail for nested tags.
Definition at line 3520 of file qregexp.cpp. References QRegExpPrivate::min, and priv. Referenced by fixEnvVariables(), and MingwMakefileGenerator::writeSubDirs(). |
|
|
Sets the pattern string to pattern. The case sensitivity, wildcard and minimal matching options are not changed.
Definition at line 3425 of file qregexp.cpp. References caseSensitive(), compile(), QRegExpPrivate::pattern, and priv. Referenced by SConfig::setPattern(), and StringParser::split(). |
|
|
Sets the wildcard mode for the regular expression. The default is FALSE. Setting wildcard to TRUE enables simple shell-like wildcard matching. (See wildcard matching (globbing) .)
For example, r*.txt matches the string
Definition at line 3483 of file qregexp.cpp. References caseSensitive(), compile(), priv, and QRegExpPrivate::wc. Referenced by OFileViewFileListView::compliesMime(), Opie::Ui::Internal::OFileViewFileListView::compliesMime(), and AbView::slotDoFind(). |
|
|
Returns TRUE if wildcard mode is enabled; otherwise returns FALSE. The default is FALSE.
Definition at line 3465 of file qregexp.cpp. References priv, and QRegExpPrivate::wc. |
|
|
Definition at line 111 of file qregexp.h. Referenced by operator=(), and operator==(). |
|
|
Definition at line 112 of file qregexp.h. Referenced by cap(), capturedTexts(), compile(), exactMatch(), isEmpty(), matchedLength(), minimal(), operator=(), operator==(), pattern(), pos(), QRegExp(), search(), searchRev(), setMinimal(), setPattern(), setWildcard(), wildcard(), and ~QRegExp(). |
1.4.2