Skip to content

Commit

Permalink
Merge pull request #3732 from nirvn/strpos_regexp_match_upgrade
Browse files Browse the repository at this point in the history
[expression] strpos() and regexp_match() improvements
  • Loading branch information
nyalldawson committed Nov 10, 2016
2 parents 164a85a + 6b2b4c5 commit 6ea0049
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions resources/function_help/json/regexp_match
@@ -1,9 +1,9 @@
{
"name": "regexp_match",
"type": "function",
"description": "Returns true if any part of a string matches the supplied regular expression.",
"description": "Return the first matching position matching a regular expression within a string, or 0 if the substring is not found.",
"arguments": [ {"arg":"input_string","description":"the string to test against the regular expression"},
{"arg":"regex","description":"The regular expression to test against. Backslash characters must be double escaped (eg \"\\\\\\\\s\" to match a white space character)."}
],
"examples": [ { "expression":"regexp_match('QGIS ROCKS','\\\\\\\\sROCKS')", "returns":"true"}]
"examples": [ { "expression":"regexp_match('QGIS ROCKS','\\\\\\\\sROCKS')", "returns":"4"}]
}
4 changes: 2 additions & 2 deletions src/core/qgsexpression.cpp
Expand Up @@ -1333,7 +1333,7 @@ static QVariant fcnRegexpMatch( const QVariantList& values, const QgsExpressionC
parent->setEvalErrorString( QObject::tr( "Invalid regular expression '%1': %2" ).arg( regexp, re.errorString() ) );
return QVariant();
}
return QVariant( str.contains( re ) ? 1 : 0 );
return QVariant(( str.indexOf( re ) + 1 ) );
}

static QVariant fcnRegexpMatches( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
Expand Down Expand Up @@ -1529,7 +1529,7 @@ static QVariant fcnConcat( const QVariantList& values, const QgsExpressionContex
static QVariant fcnStrpos( const QVariantList& values, const QgsExpressionContext*, QgsExpression *parent )
{
QString string = getStringValue( values.at( 0 ), parent );
return string.indexOf( QRegExp( getStringValue( values.at( 1 ), parent ) ) ) + 1;
return string.indexOf( getStringValue( values.at( 1 ), parent ) ) + 1;
}

static QVariant fcnRight( const QVariantList& values, const QgsExpressionContext*, QgsExpression *parent )
Expand Down
3 changes: 2 additions & 1 deletion tests/src/core/testqgsexpression.cpp
Expand Up @@ -852,6 +852,7 @@ class TestQgsExpression: public QObject
QTest::newRow( "regexp_matches no capturing group" ) << "regexp_matches('some string','.*')" << false << QVariant( QVariantList() );
QTest::newRow( "regexp_matches invalid" ) << "regexp_matches('invalid','(')" << true << QVariant();
QTest::newRow( "strpos" ) << "strpos('Hello World','World')" << false << QVariant( 7 );
QTest::newRow( "strpos non-regexp" ) << "strpos('Hello.World','.')" << false << QVariant( 6 );
QTest::newRow( "strpos outside" ) << "strpos('Hello World','blah')" << false << QVariant( 0 );
QTest::newRow( "left" ) << "left('Hello World',5)" << false << QVariant( "Hello" );
QTest::newRow( "right" ) << "right('Hello World', 5)" << false << QVariant( "World" );
Expand Down Expand Up @@ -911,7 +912,7 @@ class TestQgsExpression: public QObject
QTest::newRow( "coalesce exp" ) << "coalesce(NULL, 1+1)" << false << QVariant( 2 );
QTest::newRow( "regexp match" ) << "regexp_match('abc','.b.')" << false << QVariant( 1 );
QTest::newRow( "regexp match invalid" ) << "regexp_match('abc DEF','[[[')" << true << QVariant();
QTest::newRow( "regexp match escaped" ) << "regexp_match('abc DEF','\\\\s[A-Z]+')" << false << QVariant( 1 );
QTest::newRow( "regexp match escaped" ) << "regexp_match('abc DEF','\\\\s[A-Z]+')" << false << QVariant( 4 );
QTest::newRow( "regexp match false" ) << "regexp_match('abc DEF','\\\\s[a-z]+')" << false << QVariant( 0 );
QTest::newRow( "if true" ) << "if(1=1, 1, 0)" << false << QVariant( 1 );
QTest::newRow( "if false" ) << "if(1=2, 1, 0)" << false << QVariant( 0 );
Expand Down

0 comments on commit 6ea0049

Please sign in to comment.