2022-03-02 09:51:03 +01:00
|
|
|
|
using System.Text.RegularExpressions; // Regex
|
|
|
|
|
|
|
|
|
|
|
|
Write("Enter your age: ");
|
|
|
|
|
|
string input = ReadLine()!; // null-forgiving
|
|
|
|
|
|
|
2022-09-18 21:50:52 +02:00
|
|
|
|
Regex ageChecker = DigitsOnly();
|
2022-03-02 09:51:03 +01:00
|
|
|
|
|
|
|
|
|
|
if (ageChecker.IsMatch(input))
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteLine("Thank you!");
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteLine($"This is not a valid age: {input}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-07-08 17:05:38 +02:00
|
|
|
|
// C# 1 to 10: Use escaped double-quote characters \"
|
|
|
|
|
|
// string films = "\"Monsters, Inc.\",\"I, Tonya\",\"Lock, Stock and Two Smoking Barrels\"";
|
|
|
|
|
|
|
|
|
|
|
|
// C# 11 or later: Use """ to start and end a raw string literal
|
|
|
|
|
|
string films = """
|
|
|
|
|
|
"Monsters, Inc.","I, Tonya","Lock, Stock and Two Smoking Barrels"
|
|
|
|
|
|
""";
|
2022-03-02 09:51:03 +01:00
|
|
|
|
|
|
|
|
|
|
WriteLine($"Films to split: {films}");
|
|
|
|
|
|
|
|
|
|
|
|
string[] filmsDumb = films.Split(',');
|
|
|
|
|
|
|
|
|
|
|
|
WriteLine("Splitting with string.Split method:");
|
|
|
|
|
|
foreach (string film in filmsDumb)
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteLine(film);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-18 21:50:52 +02:00
|
|
|
|
Regex csv = CommaSeparator();
|
2022-03-02 09:51:03 +01:00
|
|
|
|
|
|
|
|
|
|
MatchCollection filmsSmart = csv.Matches(films);
|
|
|
|
|
|
|
|
|
|
|
|
WriteLine("Splitting with regular expression:");
|
|
|
|
|
|
foreach (Match film in filmsSmart)
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteLine(film.Groups[2].Value);
|
|
|
|
|
|
}
|