Reputation: 557
In Sitecore when I add items to my Treelist I would like the treelist to only allow no items or 2 - 3 items.
In the template of the item I can set RegEx in the validation field to achieve this.
I've found this RegEx here: ^.{0,116}$
This regex allows 0-3 items. But how could I not allow 1?
Update: Edited my first question to be more exact into my problem. Sorry..
Upvotes: 0
Views: 1363
Reputation: 557
Figured out the answer to my question.
This would validate correctly:
^(.{0}|.{77,116})$
If anyone has a better answer, please submit. Thanks for the help!
Upvotes: 1
Reputation: 10605
The answer is "yes" (see deceze's answer). But, you are adding to a list, and adding nothing to the list is a noop (unless you are adding a null entry?). You could simply look for 2 or 3 (...{2,3}).
This is why it's always good to supply context with your question, that is when you get the best answers.
This example will add all the lines that match your pattern to the list when there are 2 or 3 matches.
var list = new List<Match>();
var textlines = @"
This is a test
*}|{*
*}|{**}|{*
*}|{**}|{**}|{*
*}|{**}|{**}|{**}|{**}|{**}|{*
";
var pattern = @"^(\*\}\|\{\*){2,3}$";
var mx = Regex.Matches(textlines, pattern, RegexOptions.Multiline);
foreach (Match m in mx)
list.Add(m);
However, the sample data is contrived based upon your pattern. Is this really how your data looks? Or could it be you are looking for something more like...
var list = new List<Match>();
var textlines = @"
This is a test
*}|{*
*}|{*blah blah blah*}|{*
*}|{*blah blah blah*}|{*blah blah blah*}|{*
*}|{*blah blah blah*}|{*blah blah blah*}|{*blah blah blah*}|{*blah blah blah*}|{*blah blah blah*}|{*
";
var pattern = @"^([^*]*\*\}\|\{\*){2,3}$"; //notice the change to the pattern
var mx = Regex.Matches(textlines, pattern, RegexOptions.Multiline);
foreach (Match m in mx)
list.Add(m);
Regardless, I hope this helps you along in some way.
Upvotes: 0
Reputation: 64865
You can do something like: A(|foo{2,3})B which matches AB, AfoofooB, and AfoofoofooB.
However, you really should consider that perhaps not using regex for counting the number of matches would generally be better.
Upvotes: 0
Reputation: 522210
(...{2,3})?
Make your expression match 2 or 3 items and make the whole expression optional using ?
.
Upvotes: 6