1

I have a stash list of different plans that I need to break based on whether the word light is in the title or not.

So here is what I'd like:

  1. One list of plans whose title contains the word light. If it doesn't contain light then I don't want it.
  2. Complete opposite to above. List of plans without light in the title.

I believe this possible using the match and against params in stash but I can't work out what the regex would be.

Code:

{exp:stash:set_list
name="digismart_plans"
save="yes"
scope="site"
parse_tags="yes"
}

{exp:channel:entries
    channel="plan_digismart"
    disable="categories|category_fields|member_data|pagination|trackbacks"
    dynamic="no"
}
    {stash:plan_title}{title}{/stash:plan_title}
    {stash:plan_entry_id}{entry_id}{/stash:plan_entry_id}

{/exp:channel:entries}
{/exp:stash:set_list}

Then later in the template

{exp:stash:get_list
name="digismart_plans"
parse_vars="yes"
parse_tags="yes"
match="[#Light#]"
against="plan_title"
}
    {plan_title}
{/exp:stash:get_list}

Not sure on the regex required for not Light. Would be as simple as match="{#!Light#]"?

2
  • UPDATE: Just updated the final code example to have the correct against="" parameter value.
    – since1976
    Jan 29 '13 at 3:04
  • 1
    Nate's excellent answer below is what you need, but I'd just point out that you don't need the parse_tags="yes" or parse_vars="yes" when you get the list. Also, be sure to enclose your regex pattern with hashes, e.g. #(?m)^((?!\b(l|L)ight\b).)*$# Jan 29 '13 at 19:49
4

Inverse Matching


(?m)^((?!\bLight\b).)*$

You can see it in action here and take note of the info in the shot. Everything highlighted is being matched or in this case not matching Light:

Patterns

If you need to match both lowercase and capital light then you'd change it to this. Everything highlighted is being matched or in this case not matching Light or light:

(?m)^((?!\b(l|L)ight\b).)*$

And you end up with:

Patterns RegExp

Matching


\bLight\b matches Light:

patterns regex

And finally \b(l|L)ight\b matches both light and Light:

regex

Note: Notice how we aren't picking up Lighttt? That's because we are wrapping with \b which sets up our match boundaries. Remove the two \b if you don't need boundaries.

Update: as pointed out by Mark Croxton in a comment:

Also, be sure to enclose your regex pattern with hashes, e.g. #(?m)^((?!\b(l|L)ight\b).)*$#

I'm sure Mark can explain this in more detail but the basic gist of it is that Stash itself reads regex wrapped in the hashes. This is something that, to my understanding anyway, pertains to Stash only; versus everyday regex.

4
  • 1
    ^ What Nate said :) Jan 29 '13 at 8:54
  • Patterns is great for testing that kind of stuff out ;o) Jan 29 '13 at 10:06
  • Nate, you sir are a legend. Thank you so much. Will check out Patterns app too.
    – since1976
    Jan 29 '13 at 20:27
  • Honestly Mark is the legend here! In fact if it wasn't for him and Stash I would have never tried learning regex. He encouraged me one day to give it a shot and funny thing is I found it kind of fun. Like trying to solve a puzzle. Patterns is a great tool to use if you are on Mac. I'm sure there are some for other platforms as well. I know there are quite a few free online version which can be had via a quick Google Search for "regex tester" etc.
    – Natetronn
    Jan 29 '13 at 21:59
0

I'm no regex guru, far from it - more a regex googler

A quick Google suggests that using negative look arounds to do an inverse match.

^((?!Light).)*$

Should work

Also when using match against, you might have to use the stash title not the original 'title'

against="plan_title"

Either way this should, hopefully, help you on your way :)

1
  • Sorry that was a bad copy paste operation on my part. I am actually trying to match against="plan_title" as suggested. Still it pulls out all items in the list.
    – since1976
    Jan 29 '13 at 3:03

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.