diff --git a/src/core/expression/qgsexpressionfunction.cpp b/src/core/expression/qgsexpressionfunction.cpp index 4b0cc5815d0..717e32ce8b2 100644 --- a/src/core/expression/qgsexpressionfunction.cpp +++ b/src/core/expression/qgsexpressionfunction.cpp @@ -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 { diff --git a/tests/src/core/testqgsexpression.cpp b/tests/src/core/testqgsexpression.cpp index 3c7547feef3..0fcf2f065d2 100644 --- a/tests/src/core/testqgsexpression.cpp +++ b/tests/src/core/testqgsexpression.cpp @@ -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();