Question

In: Computer Science

C# & ASP.NET Create a console application that prompts the user to enter a regular expression,...

C# & ASP.NET

Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc:

The default regular expression checks for at least one digit.
Enter a regular expression (or press ENTER to use the default): ^[a- z]+$
Enter some input: apples
apples matches ^[a-z]+$? True
Press ESC to end or any key to try again.
Enter a regular expression (or press ENTER to use the default): ^[a- z]+$
Enter some input: abc123xyz
abc123xyz matches ^[a-z]+$? False
Press ESC to end or any key to try again.

Solutions

Expert Solution

The Console program is given below. The comments are provided for the better understanding of the logic.

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main(string[] args)
    {
        string inputRegex = "";
        string inputString = "";

        //Display initial message
        Console.WriteLine("The default regular expression checks for at least one digit.");
        
        //Start a continuous loop.  The exit condition is taken care inside the loop.
        while (true)
        {
            //Read the regex pattern from the console.
            Console.Write("\nEnter a regular expression (or press ENTER to use the default): ");
            inputRegex = Console.ReadLine();
            //If Enter key is pressed, default the regex pattern to atleast 1 digit.
            if (inputRegex == "")
                inputRegex = "[0-9]+";

            //Read some input
            Console.Write("Enter some input: ");
            inputString = Console.ReadLine();

            //Check if the input matches the regex pattern entered.
            bool isMatchFound = Regex.IsMatch(inputString, inputRegex);

            //Display the message whether it matches or not.
            Console.WriteLine("{0} matches {1} ? {2}", inputString, inputRegex, isMatchFound);

            //Read a key from the user.
            Console.Write("\nPress ESC to end or any key to try again.");
            ConsoleKeyInfo key = Console.ReadKey();

            //If the Escape key is pressed, break the loop. Otherwise continue with the loop.
            if (key.Key == ConsoleKey.Escape)
                break;
        }
    }
}

The screenshots of the code and output are provided below.


Related Solutions

C# Create a console application that prompts the user to enter a regular expression, and then...
C# Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a regular expression...
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
Write a C++ console program that prompts a user to enter information for the college courses...
Write a C++ console program that prompts a user to enter information for the college courses you have completed, planned, or are in progress, and outputs it to a nicely-formatted table. Name the CPP as you wish (only use characters, underscores and number in your file name. DO NOT use blank). Its output should look something like this: Course Year Units Grade ---------- ---- ----- ----- comsc-110 2015 4 A comsc-165 2016 4 ? comsc-200 2016 4 ? comsc-155h 2014...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
C# Create an application that asks the user to enter their new password. The password must...
C# Create an application that asks the user to enter their new password. The password must be at least 6 characters long. Develop a custom exception handling case that checks for the password length. (hint: use " .Length " built-in method). If the new password is less than 6 characters long the custom exception should output an error message.
C++ Write the C++ code for a void function that prompts the user to enter a...
C++ Write the C++ code for a void function that prompts the user to enter a name, and then stores the user's response in the string variable whose address is passed to the function. Name the function getName.
( USE C++ ) The program prompts the user to enter a word. The program then...
( USE C++ ) The program prompts the user to enter a word. The program then prints out the word with letters in backward order. For example, if the user enter "hello" then the program would print "olleh" show that it works .
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Create a CubesSum application that prompts the user for a non-negative integer and then displays the...
Create a CubesSum application that prompts the user for a non-negative integer and then displays the sum of the cubes of the digits.   b) Modify the application to determine what integers of two, three, and four digits are equal to the sum of the cubes of their digits.(Java Programming )
Create a program that when run, prompts the user to enter a city name, a date,...
Create a program that when run, prompts the user to enter a city name, a date, the minimum temperature and the maximum temperature. Calculate the difference between the maximum temperature and the minimum temperature. Calculate the average temperature based on the minimum and maximum temperature. Print out the following: City name today's Date minimum temperature maximum temperature average temperature difference between minimum and maximum The following is a sample run, user input is shown in bold underline. Enter City Name:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT