5

When trying to find a strpos() of a substring containing curly brackets (or brackets anyway as far as I can see) I get 0 as a result.

Test for substring without {} works fine:

enter image description here

Test for substring with {} weird result (should be 6):

enter image description here

Finding the brackets seems no problem, because replace() works as expected:

enter image description here

Aren't brackets treated as normal characters in strpos(), am I missing something? Bug? Feature?

  • i see that too (QGIS 2.18.3). escaping them with \ doesn't work either, nor does doubling them up {{}}. if replace was also affected i'd suggest the curly brackets might have some special meaning to the expression parser, but that seems unlikely if replace is unaffected. worth raising a ticket? – Steven Kay May 18 '17 at 20:28
7

You need to double escape the brackets (this applies to any kind of bracket):

strpos('This {is} s a test', '\\{is\\}')

enter image description here

Tested on QGIS 2.18.7.

  • 1
    ha! that might explain why strpos( 'test (foo)' ,'[foo]') equals 6 , if the brackets are 'invisible' :-) – Steven Kay May 18 '17 at 20:36
  • 2
    @StevenKay, yes. If brackets are not double escaped, only the content inside them is read (so, 6 is the correct result in your example). However, the latter seems to work only for () and [], not for {}. – mgri May 18 '17 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.