Qt Toolkit - QRegExp Class

# QRegExp Class Reference

The QRegExp class provides pattern matching using regular expressions and wildcards. More...

`#include <qregexp.h>`

List of all member functions.

## Public Members

- QRegExp ()
- QRegExp ( const char *, bool caseSensitive=TRUE, bool wildcard=FALSE )
- QRegExp ( const QRegExp & )
- ~QRegExp ()
- QRegExp& operator= ( const QRegExp & )
- QRegExp& operator= ( const char * pattern )
- bool operator== ( const QRegExp & ) const
- bool operator!= ( const QRegExp & r ) const
- bool isEmpty () const
- bool isValid () const
- bool caseSensitive () const
- void setCaseSensitive ( bool )
- bool wildcard () const
- void setWildcard ( bool )
- const char* pattern () const
- int match ( const char * str, int index=0, int * len=0 ) const

## Protected Members

- void compile ()
- char* matchstr ( ushort *, char *, char * ) const

## Detailed Description

The QRegExp class provides pattern matching using regular expressions and wildcards.

QRegExp knows these regexp primitives:

- c matches the character 'c'
- . matches any character
- ^ matches start of input (except [^x] which matches NOT [x])
- $ matches end of input
- [] matches a set of characters, for example [a-z0-9_]
- a* matches a sequence of zero or more a's
- a+ matches a sequence of one or more a's
- a? matches an optional a
- \c escape code for matching special characters like \, [, *, +, . etc.
- \b matches the BELL character (7)
- \t matches the TAB character (9)
- \n matches newline (10)
- \r matches return (13)
- \s matches white space (9,10,11,12,13,32)
- \x12 matches the character 0x12 (18 decimal, 12 hexadecimal).
- \022 matches the character 022 (18 decimal, 22 octal).

In wildcard mode, it only knows three primitives:

- ? matches any character
- * matches any sequence of characters
- [] matches a defined set of characters, e.g. [a-zA-Z0-9\.] matches upper and lower case ASCII letters, digits, and dot, and [^z] matches everything except lower-case z.

When writing regular expressions in C++ code, remember that the C++ preprocessor processes \ characters. So in order to match a "." character, you must write "\\." in C++ source, not "\.".

Examples: qmag/qmag.cpp

## Member Function Documentation

### QRegExp::QRegExp ()

Constructs an empty regular expression.

### QRegExp::QRegExp ( const QRegExp & r )

Constructs a regular expression which is a copy of r.

See also: operator=(const and QRegExp&).

### QRegExp::QRegExp ( const char * pattern, bool caseSensitive=TRUE, bool wildcard=FALSE )

Constructs a regular expression.

Arguments:

- pattern is the regular expression pattern string.
- caseSensitive specifies whether or not to use case sensitive matching.
- wildcard specifies whether the pattern string should be used for wildcard matching (also called globbing expression), normally used for matching file names.

See also: setWildcard().

### QRegExp::~QRegExp ()

Destroys the regular expression and cleans up its internal data.

### bool QRegExp::caseSensitive () const

Returns TRUE if case sensitivity is enabled, otherwise FALSE. The default is TRUE.

See also: setCaseSensitive().

### void QRegExp::compile () `[protected]`

For internal use only.

### bool QRegExp::isEmpty () const

Returns TRUE if the regexp is empty.

### bool QRegExp::isValid () const

Returns TRUE if the regexp is valid, or FALSE if it is invalid.

The pattern "[a-z" is an example of an invalid pattern, since it lacks a closing bracket.

### int QRegExp::match ( const char * str, int index=0, int * len=0 ) const

Attempts to match in str, starting from position index. Returns the position of the match, or -1 if there was no match.

If len is not a null pointer, the length of the match is stored in *len.

Example:

        QRegExp r("[0-9]*\.[0-9]+");                // matches floating point
        int len;
        r.match("pi = 3.1416", 0, &len);            // returns 5, len == 6

Examples: qmag/qmag.cpp

### char * QRegExp::matchstr ( ushort * rxd, char * str, char * bol ) const `[protected]`

For internal use only.

### bool QRegExp::operator!= ( const QRegExp & r ) const

Returns TRUE if this regexp is not equal to r.

See also: operator==().

### QRegExp & QRegExp::operator= ( const QRegExp & r )

Copies the regexp r and returns a reference to this regexp. The case sensitivity and wildcard options are copied, as well.

### QRegExp & QRegExp::operator= ( const char * pattern )

Sets the pattern string to pattern and returns a reference to this regexp. The case sensitivity or wildcard options do not change.

### bool QRegExp::operator== ( const QRegExp & r ) const

Returns TRUE if this regexp is equal to r.

Two regexp objects are equal if they have equal pattern strings, case sensitivity options and wildcard options.

### const char * QRegExp::pattern () const

Returns the pattern string of the regexp.

### void QRegExp::setCaseSensitive ( bool enable )

Enables or disables case sensitive matching.

In case sensitive mode, "a.e" matches "axe" but not "Axe".

See also: caseSensitive().

### void QRegExp::setWildcard ( bool wildcard )

Sets the wildcard option for the regular expression. The default is FALSE.

Setting wildcard to TRUE makes it convenient to match filenames instead of plain text.

For example, "qr*.cpp" matches the string "qregexp.cpp" in wildcard mode, but not "qicpp" (which will be matched in normal mode).

See also: wildcard().

### bool QRegExp::wildcard () const

Returns TRUE if wildcard mode is on, otherwise FALSE.

See also: setWildcard().

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qregexp.h: 1998/08/19
- qregexp.cpp: 1998/11/26