I have a shapefile containing a String Type Field called 'Class'. This field contains one or more integers separated by commas.

I want to create Rule-Based symbology matching only 2, 12, 1 respectively.

I've tried the following rules with no luck, for the string '1,2,12' each rule matches both 2 + 12 e.g.

Rule 1 - 'Class' LIKE %2%

Rule 2 - regexp_match('Class','2')

Rule-Based Symbology Matching both 2 + 12

I was thinking adding * before and after each integer when the field is being populated but that would make the field very long. Was hoping there would be a fix for this problem.

  • 1
    something like regexp_match("Class", '\\b2\\b') - might work. I'm not sure if QGIS supports word boundaries (\b)
    – Ian Turton
    Aug 18, 2019 at 9:08
  • I'm sure not to understand but ... how many classes do u want ? do u mean 3 ? how do u want to treat the cases where more than one integer are in your string ? the rules u showed (rule1 and rule2) won't discriminate 2 and 12 that's awaited ... u want something generic or just working for the dataset you're showing ?
    – Snaileater
    Aug 18, 2019 at 12:17
  • how about regexp_match('Class',',2') OR regexp_match('Class','2,')
    – csk
    Aug 19, 2019 at 17:58

1 Answer 1


How about something like:

array_contains(string_to_array("Class", ','), '2')

This splits the string in the field "Class" into an array and returns true if the array contains the given value ('2'). This will give you all the features that contain the class/integer 2 (and leave out 12).

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.