# regex 0 or 5+

Page 1 of 1

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

### #1 andrewsw

Reputation: 6833
• Posts: 28,341
• 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

Reputation: 6833
• Posts: 28,341
• 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.

### #3 sepp2k

• D.I.C Lover

Reputation: 2757
• Posts: 4,419
• 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

### #4 Skydiver

• Code herder

Reputation: 7230
• Posts: 24,513
• 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.

### #5 andrewsw

Reputation: 6833
• Posts: 28,341
• 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.

### #6 Skydiver

• Code herder

Reputation: 7230
• Posts: 24,513
• 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

### #7 Skydiver

• Code herder

Reputation: 7230
• Posts: 24,513
• 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)\$

```

### #8 andrewsw

Reputation: 6833
• Posts: 28,341
• Joined: 12-December 12

## Re: regex 0 or 5+

Posted 30 August 2019 - 12:07 AM

Skydiver, 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.

### #9 maceysoftware

• Member Title

Reputation: 394
• Posts: 1,649
• 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?

### #10 andrewsw

Reputation: 6833
• Posts: 28,341
• 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()
{
@"^(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.

### #11 maceysoftware

• Member Title

Reputation: 394
• Posts: 1,649
• 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.

### #12 Skydiver

• Code herder

Reputation: 7230
• Posts: 24,513
• 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.