Regular expression for the interval of years

In C #, I want to write a regular expression that will only take years between 1900 and 2099.

I tried ^([1][9]\d\d|[2][0]\d\d)$, but it does not work. Any ideas?

So I have a class:

    [NotNullValidator(MessageTemplate = "Anul nu poate sa lipseasca!")]
  //  [RangeValidator(1900, RangeBoundaryType.Inclusive, 2100, RangeBoundaryType.Inclusive, MessageTemplate = "Anul trebuie sa contina 4 caractere!")]
    [RegexValidator(@"(19|20)\d{2}$", MessageTemplate = "Anul trebuie sa fie valid!", Ruleset = "validare_an")]
    public int anStart
    {
        get;
        set;
    }

And in the testing method:

[TestMethod()]
public void anStartTest()
{
    AnUnivBO target = new AnUnivBO() { anStart = 2009 };
    ValidationResults vr = Validation.Validate<AnUnivBO>(target, "validare_an");
    Assert.IsTrue(vr.IsValid);
}

Why is this failing?

+3
source share
5 answers

For RegexValidator to work, you must use the string property, not an integer:

public string anStart
{
    get;
    set;
}

In your testing method, you will need to use:

AnUnivBO target = new AnUnivBO() { anStart = "2009" };

To continue using the integer, use RangeValidator :

[RangeValidator(1900, RangeBoundaryType.Inclusive,
                2099, RangeBoundaryType.Inclusive)]
public anStartint anStart 
{
   get; set;
)
+2
source

Try the following:

^(19|20)\d{2}$
+3
source

[],

/^(19\d\d|20\d\d)$/

, . if(date <= 2099 && date>=1900)

+2

:

^ ((19\d\d) | (20\d\d)) $

0
source

It works in Python ^(19|20)\d\d$.

>>> import re
>>> pat=re.compile("^(19|20)\\d\\d$")
>>> print re.match(pat,'1999')
<_sre.SRE_Match object at 0xb7c714a0>
>>> print re.match(pat,'2099')
<_sre.SRE_Match object at 0xb7c714a0>
>>> print re.match(pat,'1899')
None
>>> print re.match(pat,'2199')
None
>>> print re.match(pat,'21AA')
None
0
source

Source: https://habr.com/ru/post/1729718/


All Articles