Hi all,
I'm having problems understanding errors within a RegEx class I've got: (Please bare with me, I'm not an experienced C# programmer).
The class works (or did work when passing arguments from the command line using the .exe generated by the class.
What I have try to do is pass the class string values directly from the program page itself.
Here is the program page:
C#
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace RegEx_TestCApp_Recipe02_05
{
class Program
{
const String REGEX_STRING_VALUE = @"^[\w-]+@([\w-]+\.)+\w-]+$ ";
const String REGEX_STRING_EMAIL = @"[email protected]";
//string line;
//Readline
static void Main(string[] args)
{
//Console.WriteLine(REGEX_STRING_VALUE);
//System.Threading.Thread.Sleep(10000);
Recipe02_05 Receipe_Class_test;
// (@"^[\w-]+@([\w-]+\.)+\w-]+$ [email protected]");
Receipe_Class_test = new Recipe02_05(REGEX_STRING_VALUE + REGEX_STRING_EMAIL);
//Receipe_Class_test = new Recipe02_05();
//Receipe_Class_test.(REGEX_STRING_VALUE, REGEX_STRING_EMAIL);
//Console.WriteLine("Regular Expression: {0}", args[0]);
//Console.WriteLine("Input: {0}", args[1]);
//Console.WriteLine("Valid = {0}", ValidateInput(args[0], args[1]));
//Console.WriteLine("\nMain method complete. Press Enter");
//Console.ReadLine();
}
}
}
Here is the Class page
using System;
using System.Text.RegularExpressions;
namespace RegEx_TestCApp_Recipe02_05
{
class Recipe02_05
{
/*
// Alternative version of the ValidateInput method that does not create Regex instances.
public static bool ValidateInput(string regex, string input)
{
// Test if the specified input matches the regular expression.
return Regex.IsMatch(input, regex);}*/
C#
public static bool ValidateInput(string regex, string input)
{
// Create a new Regex based on the specified regular expression.
Regex r = new Regex(regex);
// Test if the specified input matches the regular expression.
return r.IsMatch(input);
}
//public static void Main(string[] args)
//{
// Test the input from the command line. The first argument is the
// regular expression, and the second is the input.
//Console.WriteLine("Regular Expression: {0}", args[0]);
//Console.WriteLine("Input: {0}", args[1]);
//Console.WriteLine("Valid = {0}", ValidateInput(args[0], args[1]));
// Wait to continue.
//Console.WriteLine("\nMain method complete. Press Enter");
//Console.ReadLine();
//}
}
}
Bechir BejaouiRank #144 · 15.9K pointsPosted Sep 6, 2008, 2:21 AM EDT
try this instead:
class Program
{
const String REGEX_STRING_VALUE = @"^[\w-]+@([\w-]+\.)+\w-]+$ ";
const String REGEX_STRING_EMAIL = @"[email protected]";
static void Main(string[] args)
{
Recipe02_05 Receipe_Class_test;
Receipe_Class_test = new Recipe02_05(REGEX_STRING_VALUE, REGEX_STRING_EMAIL);
Console.Read();
}
}
class Recipe02_05
{
public Recipe02_05(string regex, string input)
{
// Create a new Regex based on the specified regular expression.
Regex r = new Regex(regex);
// Test if the specified input matches the regular expression.
if (r.IsMatch(input))
{
//TODO: Perform the code when the validation occurs
}
else
{
//TODO: Perform the code when the validation doesn't occur
}
}
}