11 Replies - 4135 Views - Last Post: 30 August 2019 - 08:32 AM

#1 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,186
  • Joined: 12-December 12

regex 0 or 5+

Posted 29 August 2019 - 01:19 AM

Creating regex for a range of numeric values is "tricky", but I also want to allow 0 as a distinct option. I need an alternation for this.

So I am looking to create a regex for:
0 or a number from 5 upwards

If it helps I can optionally set a maximum, so 0 or between 5 and 480.

Do you have some advise or assistance with this?
Is This A Good Question/Topic? 0
  • +

Replies To: regex 0 or 5+

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,186
  • Joined: 12-December 12

Re: regex 0 or 5+

Posted 29 August 2019 - 01:36 AM

Mm I might go with

^(0|[5-9]|[1-9][0-9]|[1-9][0-9][0-9])$


which allows 0 or the range 5-999; I don't think the upper limit is overly important, but 999 is an acceptable maximum.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2745
  • View blog
  • Posts: 4,405
  • Joined: 21-June 11

Re: regex 0 or 5+

Posted 29 August 2019 - 04:16 AM

You can get rid of the maximum and just use a quantifier on the 0-9:

^(0|[5-9]|[1-9][0-9]+)$


If you want you can also allow leading zeros by adding 0* at the start.

That said, it might be easier/more readable to just convert the input to an integer and then check i == 0 || i >= 5.

This post has been edited by sepp2k: 29 August 2019 - 04:28 AM

Was This Post Helpful? 2
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: regex 0 or 5+

Posted 29 August 2019 - 05:11 AM

Lol! Regex is for strings, not integers. :)

Since this is a C# question, presumably you are trying to the validation in C#, and not someplace client or UI side where you can only use regex.
Was This Post Helpful? 1
  • +
  • -

#5 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,186
  • Joined: 12-December 12

Re: regex 0 or 5+

Posted 29 August 2019 - 05:30 AM

Hi and thanks both.

It is in Silverlight and they use some CSLA business rules nonsense, so I am kinda restricted to regex.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: regex 0 or 5+

Posted 29 August 2019 - 07:53 AM

Is this CSLA the same as this same CSLA.NET that claims:

Quote

Able to access an understandable, but extremely flexible and powerful, rules engine where you can implement all your validation, business, algorithmic, and authorization rules in a totally reusable manner

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: regex 0 or 5+

Posted 29 August 2019 - 08:18 AM

Hmmm... ugly:
^(0|
  [5-9]|
  [1-9][0-9]|
  [1-3][0-9][0-9]|
  4[0-7][0-9]|
  480)$


Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,186
  • Joined: 12-December 12

Re: regex 0 or 5+

Posted 30 August 2019 - 12:07 AM

View PostSkydiver, on 29 August 2019 - 02:53 PM, said:

Is this CSLA the same as this same CSLA.NET that claims:

Quote

Able to access an understandable, but extremely flexible and powerful, rules engine where you can implement all your validation, business, algorithmic, and authorization rules in a totally reusable manner

I think that's the chap.
Was This Post Helpful? 0
  • +
  • -

#9 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 380
  • View blog
  • Posts: 1,618
  • Joined: 07-September 13

Re: regex 0 or 5+

Posted 30 August 2019 - 12:35 AM

You can create a custom BusinessRule with CSLA and then validate them as Skydiver has said?
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6813
  • View blog
  • Posts: 28,186
  • Joined: 12-December 12

Re: regex 0 or 5+

Posted 30 August 2019 - 02:33 AM

Maybe it is a different or older version of CSLA. Anyway, I used code as follows

 protected override void AddBusinessRules()
        {
            BusinessRules.AddRule(new Csla.Rules.CommonRules.RegExMatch(performSyncEveryXMinutes,
                @"^(0|[5-9]|[1-9][0-9]|[1-9][0-9][0-9])$", "Either 0 (off) or between 5 and 999"));
        }



Thank you for the help.
Was This Post Helpful? 0
  • +
  • -

#11 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 380
  • View blog
  • Posts: 1,618
  • Joined: 07-September 13

Re: regex 0 or 5+

Posted 30 August 2019 - 03:17 AM

Where you have AddRule, you don't have to pass through one of CSLA defined rules, instead, you can create a new Custom Rule which inherits (At Least, Csla.Rules.BusinessRule) and then pass through an instance like you are, but for your custom class.
Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7024
  • View blog
  • Posts: 23,849
  • Joined: 05-May 12

Re: regex 0 or 5+

Posted 30 August 2019 - 08:32 AM

Looks like there is a MaxValue rule, as well as a Lambda rule.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1