How about?
(\w+)
[^:]+)?,(\w+)
[^:]+)?,(\w+)
[^:]+)?
Go to
http://www.organicbit.com/regex/fog0000000019.html and get the regex
tool, it's handy for building these things.
The tool helps when you are coding the regex, but it is cumbersome when you
want to verify the correctness of the regex and match, across a large set of
input. For this you would be better off with a unit test app, where you
store an array of (input,output) pairs. Then run the regex on each input
and compare it to the expected output. (Example below)
-Dino
//
// emailValidation.cs
//
// uses a regexp to validate emails.
// This test program uses xml serialization to get the test input,
// including the regexp string and the various emails to test.
//
// references:
//
http://homepage.stts.edu/~agushen/script/emailvalidation.html
//
// Fri, 15 Aug 2003 11:28
//
using Ionic.Test.EmailValidation;
namespace Ionic.Test.EmailValidation {
/// <remarks>
/// Represents all the input for the test, including the regex to test,
/// and an array of test cases.
/// </remarks>
[System.Xml.Serialization.XmlRootAttribute("Email.Validation.Input",
Namespace="", IsNullable=false)]
public class TestInput {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSche
maForm.Unqualified)]
public string Regexp;
/// <remarks/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchema
Form.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("Case",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
public TestCase[] TestList;
}
/// <remarks>
/// This is the type that stores a single test case.
/// We need a bunch of these to verify that the regex works as
/// expected. Each test case has an input and an output. In our
/// case, the input is a string, and the output is a bool value,
/// which indicates whether the Regex should match or not.
/// Other tests will have different input and output.
/// </remarks>
public class TestCase {
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSche
maForm.Unqualified)]
public string Input;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSche
maForm.Unqualified)]
public bool ExpectedOutput;
}
/// <remarks>
/// This is the test app. The main routine de-serializes from
/// an XML file, then runs the tests, comparing the expected
/// (or desired) output with the actual result.
/// </remarks>
public class Tester {
public static void Main() {
string InputPath= "EmailValidationInput.xml";
System.IO.FileStream fs = new System.IO.FileStream(InputPath,
System.IO.FileMode.Open);
System.Xml.Serialization.XmlSerializer s= new
System.Xml.Serialization.XmlSerializer(typeof(TestInput));
TestInput Input= (TestInput) s.Deserialize(fs);
fs.Close();
System.Text.RegularExpressions.Regex regex= new
System.Text.RegularExpressions.Regex (Input.Regexp);
foreach (TestCase tc in Input.TestList) {
System.Console.WriteLine(tc.Input +"\n " + tc.ExpectedOutput + " \\ " +
regex.IsMatch(tc.Input));
}
}
}
}
This is input data. Store this in the XML file that is de-serialized for
this test.
<Email.Validation.Input>
<TestList>
<!--
================================================================== -->
<!-- =================== True test cases
============================== -->
<!--
================================================================== -->
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>true</ExpectedOutput>
</Case>
<!--
================================================================== -->
<!-- =================== False test cases
============================= -->
<!--
================================================================== -->
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected].</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected].</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected].</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>elmo@cloud9</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>9Lives.club.org</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>@club.org</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
<Case>
<Input>
[email protected]</Input>
<ExpectedOutput>false</ExpectedOutput>
</Case>
</TestList>
<Regexp>^(\w([\.\-\w]*\w)?)@(\w([\.\-\w]*\w)*\.\w([\.\-\w]*\w)?)$</Regexp>
</Email.Validation.Input>