Exemplo n.º 1
0
            public static MultipleFruit[] ParseFromText(string text)
            {
                ReguexAttribute attr = typeof(MultipleFruit).GetTypeInfo().GetCustomAttribute <ReguexAttribute>();

                string regexPerson  = attr.Pattern;
                var    fruitMatches = Regex.Matches(text, regexPerson);

                MultipleFruit[] fruit = new MultipleFruit[fruitMatches.Count];

                for (int i = 0; i < fruitMatches.Count; i++)
                {
                    var match = fruitMatches[i];
                    int count = Int32.Parse(match.Groups[1].Value);

                    string fruitType = match.Groups[2].Value;

                    fruit[i] = new MultipleFruit(count, fruitType);
                }

                return(fruit);
            }
Exemplo n.º 2
0
            public static ColoredShape[] ParseFromText(string text)
            {
                ReguexAttribute attr = typeof(ColoredShape).GetTypeInfo().GetCustomAttribute <ReguexAttribute>();

                string regexPerson  = attr.Pattern;
                var    shapeMatches = Regex.Matches(text, regexPerson);

                ColoredShape[] shapes = new ColoredShape[shapeMatches.Count];

                for (int i = 0; i < shapeMatches.Count; i++)
                {
                    var    match = shapeMatches[i];
                    string color = match.Groups[1].Value;

                    string shape = match.Groups[2].Value;

                    shapes[i] = new ColoredShape(color, shape);
                }

                return(shapes);
            }
Exemplo n.º 3
0
            public static Officer[] ParseFromText(string text)
            {
                ReguexAttribute attr = typeof(Officer).GetTypeInfo().GetCustomAttribute <ReguexAttribute>();

                string regexPerson    = attr.Pattern;
                var    officerMatches = Regex.Matches(text, regexPerson);

                Officer[] officers = new Officer[officerMatches.Count];

                for (int i = 0; i < officerMatches.Count; i++)
                {
                    var    match = officerMatches[i];
                    string rank  = match.Groups[1].Value;

                    string surname = match.Groups[2].Value;

                    officers[i] = new Officer(rank, surname);
                }

                return(officers);
            }
Exemplo n.º 4
0
            public static Person[] ParseFromText(string text)
            {
                ReguexAttribute attr = typeof(Person).GetTypeInfo().GetCustomAttribute <ReguexAttribute>();

                string regexPerson    = attr.Pattern;
                var    personsMatches = Regex.Matches(text, regexPerson);

                Person[] people = new Person[personsMatches.Count];

                for (int i = 0; i < personsMatches.Count; i++)
                {
                    var match = personsMatches[i];

                    try
                    {
                        string genderTitle = match.Groups[1].Value;

                        string firstName = match.Groups[2].Value;
                        string lastName  = match.Groups[3].Value;

                        DateTime birthDate = DateTime.Parse(match.Groups[4].Value);

                        people[i] = new Person(firstName, lastName, genderTitle, birthDate);
                    }
                    catch (IndexOutOfRangeException)
                    {
                        Console.Write("Check indexes of matches");
                    }
                    catch (ArgumentNullException)
                    {
                        Console.Write("No text to parse for date");
                    }
                    catch (FormatException)
                    {
                        Console.Write("Date could not be parsed");
                    }
                }

                return(people);
            }
Exemplo n.º 5
0
            public static Hero[] ParseFromText(string text)
            {
                ReguexAttribute attr = typeof(Hero).GetTypeInfo().GetCustomAttribute <ReguexAttribute>();

                string regexPerson = attr.Pattern;
                var    heroMatches = Regex.Matches(text, regexPerson);

                Hero[] heroes = new Hero[heroMatches.Count];

                for (int i = 0; i < heroMatches.Count; i++)
                {
                    var    match = heroMatches[i];
                    string name  = match.Groups[1].Value;
                    int    str   = Int32.Parse(match.Groups[2].Value);
                    int    agi   = Int32.Parse(match.Groups[3].Value);
                    int    inte  = Int32.Parse(match.Groups[4].Value);

                    heroes[i] = new Hero(name, str, agi, inte);
                }

                return(heroes);
            }
Exemplo n.º 6
0
            public static ArithmExpression[] ParseFromText(string text)
            {
                ReguexAttribute attr = typeof(ArithmExpression).GetTypeInfo().GetCustomAttribute <ReguexAttribute>();

                string regexPerson = attr.Pattern;
                var    arexMatches = Regex.Matches(text, regexPerson);

                ArithmExpression[] arex = new ArithmExpression[arexMatches.Count];

                for (int i = 0; i < arexMatches.Count; i++)
                {
                    var match = arexMatches[i];
                    int left  = Int32.Parse(match.Groups[1].Value);

                    char sign  = match.Groups[2].Value[0];
                    int  right = Int32.Parse(match.Groups[3].Value);

                    arex[i] = new ArithmExpression(left, right, sign);
                }

                return(arex);
            }
Exemplo n.º 7
0
            public static FootballClub[] ParseFromText(string text)
            {
                ReguexAttribute attr = typeof(FootballClub).GetTypeInfo().GetCustomAttribute <ReguexAttribute>();

                string regexPerson  = attr.Pattern;
                var    clubsMatches = Regex.Matches(text, regexPerson);

                FootballClub[] clubs = new FootballClub[clubsMatches.Count];

                for (int i = 0; i < clubsMatches.Count; i++)
                {
                    var match = clubsMatches[i];

                    try
                    {
                        string name = match.Groups[1].Value;

                        string originCity     = match.Groups[2].Value;
                        uint   foundationYear = UInt32.Parse(match.Groups[3].Value);

                        clubs[i] = new FootballClub(name, originCity, foundationYear);
                    }
                    catch (IndexOutOfRangeException)
                    {
                        Console.Write("Check indexes of matches");
                    }
                    catch (ArgumentNullException)
                    {
                        Console.Write("No text to parse for year");
                    }
                    catch (FormatException)
                    {
                        Console.Write("Year could not be parsed");
                    }
                }

                return(clubs);
            }