TASK 1:
using System.Text.RegularExpressions;
namespace WinFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
private void button1_Click(object sender, EventArgs e)
{
string input = InputText.Text.Trim();
string pattern = @"^[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?$";
Regex regex = new Regex(pattern);
if (regex.IsMatch(input))
{
MessageBox.Show($"'{input}' is a valid constant.", "Validation
Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show($"'{input}' is not a valid constant.", "Validation
Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
TASK 2:
using System.Text.RegularExpressions;
namespace WinFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string input = InputText.Text.Trim();
string pattern = @"\b(int|float|double|char)\b";
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(input);
if (matches.Count > 0)
{
string keywordsFound = "Data Type found: ";
foreach (Match match in matches)
{
keywordsFound += match.Value + ", ";
}
keywordsFound = keywordsFound.TrimEnd(',', ' '); // Remove trailing
comma and space
MessageBox.Show(keywordsFound, "Validation Result",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show($"No Data Type found in '{input}'.", "Validation
Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
HOME TASK 1:
using System.Text.RegularExpressions;
namespace WinFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
private void button1_Click(object sender, EventArgs e)
{
string input = InputText.Text.Trim();
// Ensure input length is not greater than 60
if (input.Length > 60)
{
MessageBox.Show("Input is too long. It should not be longer than 60
characters.", "Validation Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string pattern = @"^(?!.*\n)(?=.*[0-9])(?:(?:[0-9]+(?:\.[0-9]*)?)|(?:\.
[0-9]+))(?:[eE][+-]?[0-9]+)?$";
Regex regex = new Regex(pattern);
if (regex.IsMatch(input))
{
MessageBox.Show($"'{input}' is a valid floating-point number.",
"Validation Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show($"'{input}' is not a valid floating-point number.",
"Validation Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
HOME TASK 2:
using System.Text.RegularExpressions;
namespace WinFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label2_Click(object sender, EventArgs e)
{
private void button1_Click(object sender, EventArgs e)
{
string input = InputText.Text.Trim();
string pattern = @"\b[tTmM]\w*\b";
Regex regex = new Regex(pattern);
MatchCollection matches = regex.Matches(input);
if (matches.Count > 0)
{
string wordsFound = "Words found: ";
foreach (Match match in matches)
{
wordsFound += match.Value + ", ";
}
wordsFound = wordsFound.TrimEnd(',', ' '); // Remove trailing comma
and space
MessageBox.Show(wordsFound, "Words Starting with 't' or 'm'",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("No words starting with 't' or 'm' found.",
"Validation Result", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}