Share via

Regex Position exactly

Iago Mz 1 Reputation point
2022-04-30T20:23:28.143+00:00

Hello again...
Now I need that help me, please.
I need to find a regular expression that:
The column id = 9 numerics digits and the third digit = 0 or 3.
I try but don't can
'grep -E '^[[:digit:]]{9}' athletesv2.csv'

Only can to limit to 9 Digits

thank you

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.


1 answer

Sort by: Most helpful
  1. Karen Payne MVP 35,606 Reputation points Volunteer Moderator
    2022-05-02T12:26:16.437+00:00

    Consider using data annotations using a custom rule. Below I show validating a single entity, to validate a list, simple loop through the list and if validating on the fly the code allows var isValid = new ValidKeyAttribute().IsValid(value); where value is the number to validate.

    /// <summary>
    /// Custom rule for third digit
    /// </summary>
    public class ValidKeyAttribute : ValidationAttribute
    {
        public override bool IsValid(object sender)
        {
            string value = Convert.ToString(sender);
    
            if (value.Length < 9)
            {
                return false;
            }
    
            return value[2] == '0' || value[2] == '3';
        }
    }
    

    Using the following model to validate (below)

    public class Athlete
    {
        [ValidKey]
        [Required(ErrorMessage = "{0} is required")]
        public int Id { get; set; }
        [Required(ErrorMessage = "{0} is required")]
        [MaxLength(15)]
        public string FirstName { get; set; }
        [Required(ErrorMessage = "{0} is required")]
        [MaxLength(20)]
        public string LastName { get; set; }
        public override string ToString() => $"{FirstName} {LastName}";
    }
    

    Unit test

    [TestClass]
    public partial class ValidKeyTests : TestBase
    {
        [TestMethod]
        [TestTraits(Trait.Annotations)]
        public void ValidKeyTest()
        {
            // arrange
            int value = 123456789;
            Athlete athlete = new () { Id = value, FirstName = "Jim", LastName = "Adams" };
    
            // act
            EntityValidationResult result = Model.Validate(athlete);
    
            // assert
            Check.That(result.IsValid).IsTrue();
    
        }
        [TestMethod]
        [TestTraits(Trait.Annotations)]
        public void NotValidBadIdKeyTest()
        {
            // arrange
            int value = 126456789;
            Athlete athlete = new() { Id = value, FirstName = "Jim", LastName = "Adams" };
    
            // act
            EntityValidationResult result = Model.Validate(athlete);
    
            // assert
            Check.That(result.IsValid).IsFalse();
    
        }
    
        [TestMethod]
        [TestTraits(Trait.Annotations)]
        public void NotValidNoIdKeyTest()
        {
            // arrange
            Athlete athlete = new() { FirstName = "Jim", LastName = "Adams" };
            // act
            EntityValidationResult result = Model.Validate(athlete);
            // assert
            Check.That(result.IsValid).IsFalse();
    
        }
    }
    

    And we can shortcut the above with var isValid = new ValidKeyAttribute().IsValid(value);

    All source, C# 9, .NET Core code is in the following repository.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.