Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
How to effectively build a view with the following filter criteria: select records which do not contain string ".jpg" and ".gif" and ".png" and ",pdf" and .... and so on.
It is easy to create a criteria to contain those patterns, simply choose Regular Expression operator for the Filter Critera and enter those patterns separated by "|".
But how to create a criteria which does *not* contain these patterns? In MySQL it would look: NOT RLIKE '.jpg|.gif|.png|.pdf'
(effectively - meaning, without entering each pattern as a separate criteria).
Comments
Comment #1
damiankloip CreditAttribution: damiankloip commentedRegular expressions usually are a bit easier/suited to finding matches. This is more of a general issue, you should do some research online for patterns you can use for stuff like this.
Comment #2
raincloud CreditAttribution: raincloud commentedThe MySql formula
NOT RLIKE '.jpg|.gif|.png|.pdf'
is the effect of my online research. I just don't know how to reflect it in Views, because in Views there is only "LIKE" criteria and no "NOT LIKE" criteria.Comment #3
damiankloip CreditAttribution: damiankloip commentedI think this is the nature of most regular expressions. It can be done, Off the top of my head I don't know. You might want to search for more general terms, as it's not really anything to do with sql.
Comment #4
raincloud CreditAttribution: raincloud commentedViews has a "Regular Expression" filter. If you check the SQL query it produces, it is translated to RLIKE.
The opposite of RLIKE is NOT RLIKE. This operator is in MySql but is missing in Views. It is powerful because allows to eliminate many records easily. In Views, you just have to eliminate line by line using "Does not contain".
The solution would be to add another filter criteria which would simply translate to MySql NOT RLIKE. Views have negative operators for all other operators:
etc.
The only exception is Regular Expression filter. It does not have its negative form.
Comment #5
yang_yi_cn CreditAttribution: yang_yi_cn commentedI think it should be added as all the other filters (equals, contains) have the negative form to select.
Comment #6
Reg CreditAttribution: Reg commentedI recently came across this problem and modified views to do this since it makes no sense not to have it. Here's a patch against views version 7.x-3.7.
Comment #8
Reg CreditAttribution: Reg commentedThis one is a git style patch (created by svn) so hopefully the system will be fine with it now.
I noticed that there's an option to retest a patch. I assume that means you can also replace it before retesting. How do you do that? I always create a new comment with a new patch but it's waste at times, like this time where it's just a matter of giving the tester a format it wants to see.
Comment #9
Reg CreditAttribution: Reg commentedComment #11
Reg CreditAttribution: Reg commented"svn diff --git ..." doesn't do the paths the way I expected. Let's try after a little manual massaging of the file.
Comment #12
Reg CreditAttribution: Reg commentedComment #13
FerCamp CreditAttribution: FerCamp commentedpatch posted in #11 works for me. Adds a "not regular expression" operator. thanks
Comment #14
eileenmcnaughton CreditAttribution: eileenmcnaughton commentedWorks well for me too.
Comment #15
hargobind#11 works perfectly as advertised.
Comment #16
Reg CreditAttribution: Reg commentedCan we get this into the next version or at least the dev. version now?
Comment #17
xurizaemonRTBC++
Comment #18
Ludo.R#11 works for me with Views 7.x-3.11
RTBC++
Comment #19
annya CreditAttribution: annya commented#11 works. Thanks!
Comment #20
Reg CreditAttribution: Reg as a volunteer commentedLooks like this is well tested. Any chance this will go into the next version? I think this was asked once before and no answer was given. Anyone else want to see this in the next version?
Comment #21
xurizaemon@Reg / @all, maybe find a member of the Views Bug Squad and get them to review / give their OK will help?
Comment #22
Reg CreditAttribution: Reg as a volunteer commented@xurizaemon - I sent a message out to one of them, we'll see what happens.
Comment #23
hargobindSince #11 no longer applies against -dev, here's an updated patch against the latest 3.x branch. Nothing else has been changed. It also nicely applies against the latest stable release 3.13.
Unfortunately it now has to go back to Needs Review, so please review it and mark it RTBC!
Comment #24
heddnWorks. And is almost identical to #2137069: Add support for NOT RLIKE (negative regex)
Comment #25
SilviuChingaru CreditAttribution: SilviuChingaru commentedComment #26
SilviuChingaru CreditAttribution: SilviuChingaru commented+1
Comment #27
attheshow CreditAttribution: attheshow commentedPatch #23 is working for me as well.
Comment #28
andrefy CreditAttribution: andrefy commentedSince #27 no longer apply to dev after 3.20, here is an update patch against the latest 3.x branch Nothing else has been changed. It also nicely applies against the latest stable release 3.20.
Comment #29
andrefy CreditAttribution: andrefy commentedComment #30
Chris Matthews CreditAttribution: Chris Matthews as a volunteer commentedThe patch in #28 applied cleanly to the latest views 7.x-3.x-dev and works for me as well so changing to RTBC.
Comment #32
DamienMcKennaCommitted. Thank you!
Comment #34
strelkov.avIs it possible to add the condition DOES NOT CONTAIN ANY WORD, i.e. that it absolutely also works as CONTAIN ANY WORD only so that the query is not LIKE but NOT LIKE
It is just difficult for the user to define a negative regular expression , in my case I give the user to find results that contain a CAR or PLANE, but not a MOTORCYCLE or HELICOPTER
I.e. 2 filters, positive and negative
Comment #35
xurizaemonHey @strelkov.av, yes that is possible, but to get that in Views you'd need to open that as a new feature request (and help make it happen) as this issue has already been resolved.
An alternative would be to implement it via custom VIews hooks which invert the query behaviour to suit your requirements.
To get this into Views (or propose changes to other modules), I recommend a thorough read of these two links on collaborating in the issue queue - following that guidance will help you be effective.
(Also note this one took ~7 years to get done! First goal is probably to make a patch that delivers this and share it in your new feature request issue. Best of luck!)