Skip to content

Commit 6b2b4c5

Browse files
committed
[FEATURE][expression] strpos() and regexp_match() improvements
- strpos() now relies on a simple string within a string search - regexp_match() now returns pos of a matching regular expression
1 parent 164a85a commit 6b2b4c5

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"name": "regexp_match",
33
"type": "function",
4-
"description": "Returns true if any part of a string matches the supplied regular expression.",
4+
"description": "Return the first matching position matching a regular expression within a string, or 0 if the substring is not found.",
55
"arguments": [ {"arg":"input_string","description":"the string to test against the regular expression"},
66
{"arg":"regex","description":"The regular expression to test against. Backslash characters must be double escaped (eg \"\\\\\\\\s\" to match a white space character)."}
77
],
8-
"examples": [ { "expression":"regexp_match('QGIS ROCKS','\\\\\\\\sROCKS')", "returns":"true"}]
8+
"examples": [ { "expression":"regexp_match('QGIS ROCKS','\\\\\\\\sROCKS')", "returns":"4"}]
99
}

src/core/qgsexpression.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ static QVariant fcnRegexpMatch( const QVariantList& values, const QgsExpressionC
13331333
parent->setEvalErrorString( QObject::tr( "Invalid regular expression '%1': %2" ).arg( regexp, re.errorString() ) );
13341334
return QVariant();
13351335
}
1336-
return QVariant( str.contains( re ) ? 1 : 0 );
1336+
return QVariant(( str.indexOf( re ) + 1 ) );
13371337
}
13381338

13391339
static QVariant fcnRegexpMatches( const QVariantList& values, const QgsExpressionContext*, QgsExpression* parent )
@@ -1529,7 +1529,7 @@ static QVariant fcnConcat( const QVariantList& values, const QgsExpressionContex
15291529
static QVariant fcnStrpos( const QVariantList& values, const QgsExpressionContext*, QgsExpression *parent )
15301530
{
15311531
QString string = getStringValue( values.at( 0 ), parent );
1532-
return string.indexOf( QRegExp( getStringValue( values.at( 1 ), parent ) ) ) + 1;
1532+
return string.indexOf( getStringValue( values.at( 1 ), parent ) ) + 1;
15331533
}
15341534

15351535
static QVariant fcnRight( const QVariantList& values, const QgsExpressionContext*, QgsExpression *parent )

tests/src/core/testqgsexpression.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ class TestQgsExpression: public QObject
852852
QTest::newRow( "regexp_matches no capturing group" ) << "regexp_matches('some string','.*')" << false << QVariant( QVariantList() );
853853
QTest::newRow( "regexp_matches invalid" ) << "regexp_matches('invalid','(')" << true << QVariant();
854854
QTest::newRow( "strpos" ) << "strpos('Hello World','World')" << false << QVariant( 7 );
855+
QTest::newRow( "strpos non-regexp" ) << "strpos('Hello.World','.')" << false << QVariant( 6 );
855856
QTest::newRow( "strpos outside" ) << "strpos('Hello World','blah')" << false << QVariant( 0 );
856857
QTest::newRow( "left" ) << "left('Hello World',5)" << false << QVariant( "Hello" );
857858
QTest::newRow( "right" ) << "right('Hello World', 5)" << false << QVariant( "World" );
@@ -911,7 +912,7 @@ class TestQgsExpression: public QObject
911912
QTest::newRow( "coalesce exp" ) << "coalesce(NULL, 1+1)" << false << QVariant( 2 );
912913
QTest::newRow( "regexp match" ) << "regexp_match('abc','.b.')" << false << QVariant( 1 );
913914
QTest::newRow( "regexp match invalid" ) << "regexp_match('abc DEF','[[[')" << true << QVariant();
914-
QTest::newRow( "regexp match escaped" ) << "regexp_match('abc DEF','\\\\s[A-Z]+')" << false << QVariant( 1 );
915+
QTest::newRow( "regexp match escaped" ) << "regexp_match('abc DEF','\\\\s[A-Z]+')" << false << QVariant( 4 );
915916
QTest::newRow( "regexp match false" ) << "regexp_match('abc DEF','\\\\s[a-z]+')" << false << QVariant( 0 );
916917
QTest::newRow( "if true" ) << "if(1=1, 1, 0)" << false << QVariant( 1 );
917918
QTest::newRow( "if false" ) << "if(1=2, 1, 0)" << false << QVariant( 0 );

0 commit comments

Comments
 (0)