Skip to content

Commit 533030f

Browse files
committedApr 28, 2013
Add regexp_match function, improve help for regexp_replace function
1 parent 6e5221c commit 533030f

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed
 
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h3>regexp_match() function</h3>
2+
Returns true if any part of a string matches the supplied regular expression.
3+
4+
<p><h4>Syntax</h4>
5+
regexp_match(<i>string,regex</i>)</p>
6+
7+
<p><h4>Arguments</h4>
8+
<!-- List args for functions here-->
9+
<i> string</i> &rarr; is string. The string to test against the regular expression.<br>
10+
<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>
11+
12+
<p><h4>Example</h4>
13+
<!-- Show example of function.-->
14+
regexp_match('QGIS ROCKS','\\sROCKS') &rarr; 1</p>

‎resources/function_help/regexp_replace-en_US

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
Returns a string with the supplied regular expression replaced.
33

44
<p><h4>Syntax</h4>
5-
replace(<i>string,before,after</i>)</p>
5+
regexp_replace(<i>string,regex,after</i>)</p>
66

77
<p><h4>Arguments</h4>
88
<!-- List args for functions here-->
99
<i> string</i> &rarr; is string. The start string.<br>
10-
<i> before</i> &rarr; is string. The string to replace.<br>
11-
<i> after</i> &rarr; is string. The string that will replace <i>before</i><br></p>
10+
<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>
11+
<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>
1212

1313
<p><h4>Example</h4>
1414
<!-- Show example of function.-->
15-
replace('QGIS SHOULD ROCK','SHOULD','DOES') &rarr; 'QGIS DOES ROCK'</p>
15+
regexp_replace('QGIS SHOULD ROCK','\\sSHOULD\\s',' DOES ') &rarr; 'QGIS DOES ROCK'</p>

‎src/core/qgsexpression.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,21 @@ static QVariant fcnRegexpReplace( const QVariantList& values, QgsFeature* , QgsE
485485
}
486486
return QVariant( str.replace( re, after ) );
487487
}
488+
489+
static QVariant fcnRegexpMatch( const QVariantList& values, QgsFeature* , QgsExpression* parent )
490+
{
491+
QString str = getStringValue( values.at( 0 ), parent );
492+
QString regexp = getStringValue( values.at( 1 ), parent );
493+
494+
QRegExp re( regexp );
495+
if ( !re.isValid() )
496+
{
497+
parent->setEvalErrorString( QObject::tr( "Invalid regular expression '%1': %2" ).arg( regexp ).arg( re.errorString() ) );
498+
return QVariant();
499+
}
500+
return QVariant( str.contains( re ) ? 1 : 0 );
501+
}
502+
488503
static QVariant fcnSubstr( const QVariantList& values, QgsFeature* , QgsExpression* parent )
489504
{
490505
QString str = getStringValue( values.at( 0 ), parent );
@@ -1194,7 +1209,7 @@ const QStringList &QgsExpression::BuiltinFunctions()
11941209
<< "exp" << "ln" << "log10" << "log"
11951210
<< "round" << "toint" << "toreal" << "tostring"
11961211
<< "todatetime" << "todate" << "totime" << "tointerval"
1197-
<< "coalesce" << "$now" << "age" << "year"
1212+
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
11981213
<< "month" << "week" << "day" << "hour"
11991214
<< "minute" << "second" << "lower" << "upper"
12001215
<< "title" << "length" << "replace" << "regexp_replace"
@@ -1240,6 +1255,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
12401255
<< new StaticFunction( "totime", 1, fcnToTime, QObject::tr( "Conversions" ) )
12411256
<< new StaticFunction( "tointerval", 1, fcnToInterval, QObject::tr( "Conversions" ) )
12421257
<< new StaticFunction( "coalesce", -1, fcnCoalesce, QObject::tr( "Conditionals" ) )
1258+
<< new StaticFunction( "regexp_match", 2, fcnRegexpMatch, QObject::tr( "Conditionals" ) )
12431259
<< new StaticFunction( "$now", 0, fcnNow, QObject::tr( "Date and Time" ) )
12441260
<< new StaticFunction( "age", 2, fcnAge, QObject::tr( "Date and Time" ) )
12451261
<< new StaticFunction( "year", 1, fcnYear, QObject::tr( "Date and Time" ) )

‎tests/src/core/testqgsexpression.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ class TestQgsExpression: public QObject
302302
QTest::newRow( "coalesce null" ) << "coalesce(NULL)" << false << QVariant( );
303303
QTest::newRow( "coalesce mid-null" ) << "coalesce(1, NULL, 3)" << false << QVariant( 1 );
304304
QTest::newRow( "coalesce exp" ) << "coalesce(NULL, 1+1)" << false << QVariant( 2 );
305+
QTest::newRow( "regexp match" ) << "regexp_match('abc','.b.')" << false << QVariant( 1 );
306+
QTest::newRow( "regexp match invalid" ) << "regexp_match('abc DEF','[[[')" << true << QVariant();
307+
QTest::newRow( "regexp match escaped" ) << "regexp_match('abc DEF','\\\\s[A-Z]+')" << false << QVariant( 1 );
308+
QTest::newRow( "regexp match false" ) << "regexp_match('abc DEF','\\\\s[a-z]+')" << false << QVariant( 0 );
305309

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

0 commit comments

Comments
 (0)