Skip to content
Permalink
Browse files

Fix regexp_substr expression function returning whole match instead o…

…f captured group
  • Loading branch information
nyalldawson committed Nov 27, 2017
1 parent 01d1be9 commit 9bfb3f31ee9c8085240eb6dc964726956e64fc26
Showing with 12 additions and 1 deletion.
  1. +10 −1 src/core/expression/qgsexpressionfunction.cpp
  2. +2 −0 tests/src/core/testqgsexpression.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
{
@@ -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();

0 comments on commit 9bfb3f3

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