Skip to content

Commit

Permalink
Fix regexp_substr expression function returning whole match instead o…
Browse files Browse the repository at this point in the history
…f captured group
  • Loading branch information
nyalldawson committed Nov 27, 2017
1 parent 01d1be9 commit 9bfb3f3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -1205,7 +1205,16 @@ static QVariant fcnRegexpSubstr( const QVariantList &values, const QgsExpression
if ( match.hasMatch() )
{
// return first capture
return QVariant( match.captured( 0 ) );
if ( match.lastCapturedIndex() > 0 )
{
// a capture group was present, so use that
return QVariant( match.captured( 1 ) );
}
else
{
// no capture group, so using all match
return QVariant( match.captured( 0 ) );
}
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -961,6 +961,8 @@ class TestQgsExpression: public QObject
QTest::newRow( "regexp_substr non-greedy" ) << "regexp_substr('abc123','(\\\\d+?)')" << false << QVariant( "1" );
QTest::newRow( "regexp_substr no hit" ) << "regexp_substr('abcdef','(\\\\d+)')" << false << QVariant( "" );
QTest::newRow( "regexp_substr invalid" ) << "regexp_substr('abc123','([[[')" << true << QVariant();
QTest::newRow( "regexp_substr ignored part" ) << "regexp_substr('abc123','c(.)')" << false << QVariant( "1" );
QTest::newRow( "regexp_substr no capture group" ) << "regexp_substr('abc123','c\\\\d')" << false << QVariant( "c1" );
QTest::newRow( "regexp_matches" ) << "array_get(regexp_matches('qgis=>rOcks;hello=>world','qgis=>(.*)[;$]'),0)" << false << QVariant( "rOcks" );
QTest::newRow( "regexp_matches empty custom value" ) << "array_get(regexp_matches('qgis=>;hello=>world','qgis=>(.*)[;$]','empty'),0)" << false << QVariant( "empty" );
QTest::newRow( "regexp_matches no match" ) << "regexp_matches('123','no()match')" << false << QVariant();
Expand Down

0 comments on commit 9bfb3f3

Please sign in to comment.