Fix regexp_substr expression function returning whole match instead of captured group

This commit is contained in:
Nyall Dawson 2017-11-27 14:16:01 +10:00
parent 01d1be9d70
commit 9bfb3f31ee
2 changed files with 12 additions and 1 deletions

View File

@ -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
{

View File

@ -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();