Skip to content
Permalink
Browse files

Add regexp_match function, improve help for regexp_replace function

  • Loading branch information
nyalldawson committed Apr 28, 2013
1 parent 6e5221c commit 533030f80e04f8da4b1cfee7b71809ac748fa04d
@@ -0,0 +1,14 @@
<h3>regexp_match() function</h3>
Returns true if any part of a string matches the supplied regular expression.

<p><h4>Syntax</h4>
regexp_match(<i>string,regex</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> string</i> &rarr; is string. The string to test against the regular expression.<br>
<i> regex</i> &rarr; is string. The regular expression to test against. Backslash characters must be double escaped (eg "\\s" to match a white space character).<br>

<p><h4>Example</h4>
<!-- Show example of function.-->
regexp_match('QGIS ROCKS','\\sROCKS') &rarr; 1</p>
@@ -2,14 +2,14 @@
Returns a string with the supplied regular expression replaced.

<p><h4>Syntax</h4>
replace(<i>string,before,after</i>)</p>
regexp_replace(<i>string,regex,after</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> string</i> &rarr; is string. The start string.<br>
<i> before</i> &rarr; is string. The string to replace.<br>
<i> after</i> &rarr; is string. The string that will replace <i>before</i><br></p>
<i> regex</i> &rarr; is string. The regular expression to replace. Backslash characters must be double escaped (eg "\\s" to match a white space character).<br>
<i> after</i> &rarr; is string. The string that will replace any matching occurences of the supplied regular expression. Captured groups can be inserted into the replacement string using \\1, \\2, etc. <br></p>

<p><h4>Example</h4>
<!-- Show example of function.-->
replace('QGIS SHOULD ROCK','SHOULD','DOES') &rarr; 'QGIS DOES ROCK'</p>
regexp_replace('QGIS SHOULD ROCK','\\sSHOULD\\s',' DOES ') &rarr; 'QGIS DOES ROCK'</p>
@@ -485,6 +485,21 @@ static QVariant fcnRegexpReplace( const QVariantList& values, QgsFeature* , QgsE
}
return QVariant( str.replace( re, after ) );
}

static QVariant fcnRegexpMatch( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
QString regexp = getStringValue( values.at( 1 ), parent );

QRegExp re( regexp );
if ( !re.isValid() )
{
parent->setEvalErrorString( QObject::tr( "Invalid regular expression '%1': %2" ).arg( regexp ).arg( re.errorString() ) );
return QVariant();
}
return QVariant( str.contains( re ) ? 1 : 0 );
}

static QVariant fcnSubstr( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
@@ -1194,7 +1209,7 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "exp" << "ln" << "log10" << "log"
<< "round" << "toint" << "toreal" << "tostring"
<< "todatetime" << "todate" << "totime" << "tointerval"
<< "coalesce" << "$now" << "age" << "year"
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
<< "month" << "week" << "day" << "hour"
<< "minute" << "second" << "lower" << "upper"
<< "title" << "length" << "replace" << "regexp_replace"
@@ -1240,6 +1255,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "totime", 1, fcnToTime, QObject::tr( "Conversions" ) )
<< new StaticFunction( "tointerval", 1, fcnToInterval, QObject::tr( "Conversions" ) )
<< new StaticFunction( "coalesce", -1, fcnCoalesce, QObject::tr( "Conditionals" ) )
<< new StaticFunction( "regexp_match", 2, fcnRegexpMatch, QObject::tr( "Conditionals" ) )
<< new StaticFunction( "$now", 0, fcnNow, QObject::tr( "Date and Time" ) )
<< new StaticFunction( "age", 2, fcnAge, QObject::tr( "Date and Time" ) )
<< new StaticFunction( "year", 1, fcnYear, QObject::tr( "Date and Time" ) )
@@ -302,6 +302,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "coalesce null" ) << "coalesce(NULL)" << false << QVariant( );
QTest::newRow( "coalesce mid-null" ) << "coalesce(1, NULL, 3)" << false << QVariant( 1 );
QTest::newRow( "coalesce exp" ) << "coalesce(NULL, 1+1)" << false << QVariant( 2 );
QTest::newRow( "regexp match" ) << "regexp_match('abc','.b.')" << false << QVariant( 1 );
QTest::newRow( "regexp match invalid" ) << "regexp_match('abc DEF','[[[')" << true << QVariant();
QTest::newRow( "regexp match escaped" ) << "regexp_match('abc DEF','\\\\s[A-Z]+')" << false << QVariant( 1 );
QTest::newRow( "regexp match false" ) << "regexp_match('abc DEF','\\\\s[a-z]+')" << false << QVariant( 0 );

// Datetime functions
QTest::newRow( "to date" ) << "todate('2012-06-28')" << false << QVariant( QDate( 2012, 6, 28 ) );

0 comments on commit 533030f

Please sign in to comment.
You can’t perform that action at this time.