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/sc...alidation.html
//
// Fri, 15 Aug 2003 11:28
//
using Ionic.Test.Emai lValidation;
namespace Ionic.Test.Emai lValidation {
/// <remarks>
/// Represents all the input for the test, including the regex to test,
/// and an array of test cases.
/// </remarks>
[System.Xml.Seri alization.XmlRo otAttribute("Em ail.Validation. Input",
Namespace="", IsNullable=fals e)]
public class TestInput {
/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche
maForm.Unqualif ied)]
public string Regexp;
/// <remarks/>
[System.Xml.Seri alization.XmlAr rayAttribute(Fo rm=System.Xml.S chema.XmlSchema
Form.Unqualifie d)]
[System.Xml.Seri alization.XmlAr rayItemAttribut e("Case",
Form=System.Xml .Schema.XmlSche maForm.Unqualif ied, IsNullable=fals e)]
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.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche
maForm.Unqualif ied)]
public string Input;
/// <remarks/>
[System.Xml.Seri alization.XmlEl ementAttribute( Form=System.Xml .Schema.XmlSche
maForm.Unqualif ied)]
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= "EmailValidatio nInput.xml";
System.IO.FileS tream fs = new System.IO.FileS tream(InputPath ,
System.IO.FileM ode.Open);
System.Xml.Seri alization.XmlSe rializer s= new
System.Xml.Seri alization.XmlSe rializer(typeof (TestInput));
TestInput Input= (TestInput) s.Deserialize(f s);
fs.Close();
System.Text.Reg ularExpressions .Regex regex= new
System.Text.Reg ularExpressions .Regex (Input.Regexp);
foreach (TestCase tc in Input.TestList) {
System.Console. WriteLine(tc.In put +"\n " + tc.ExpectedOutp ut + " \\ " +
regex.IsMatch(t c.Input));
}
}
}
}
This is input data. Store this in the XML file that is de-serialized for
this test.
<Email.Validati on.Input>
<TestList>
<!--
=============== =============== =============== =============== ====== -->
<!-- =============== ==== True test cases
=============== =============== -->
<!--
=============== =============== =============== =============== ====== -->
<Case>
<Input>Ro***@ra bbit.com</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>th****** *************** ************@so mething.org</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>th****** *@something.9g</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>th****** *@place.org</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>We****** *****@cornell.e du</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>Ja****** *****@sun-east.com</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>Ja****** *****@sun.east. com</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>Ja****** *****@sun.com</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>Pr****** *@rolling-hills.club.org</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>9L****@c lub.org</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>fr**@som ewhere.org9</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>f@z.k</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>_e***@se same.org</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>Ha****** ****@Hogwarts.e du</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<Case>
<Input>Pr****** *************** ***@Faculty.Hog warts.edu</Input>
<ExpectedOutput >true</ExpectedOutput>
</Case>
<!--
=============== =============== =============== =============== ====== -->
<!-- =============== ==== False test cases
=============== ============== -->
<!--
=============== =============== =============== =============== ====== -->
<Case>
<Input>-e***@sesame.org </Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>el**@ses ame.org.</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>-e***@sesame.org .</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>elmo@.or g.</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>elmo@.or g</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>elmo@.so meplace.org</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>elmo@clo ud9</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>fred.@so mewhere.org9</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>fred@som ewhere..org9</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>9Lives.c lub.org</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>@club.or g</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
<Case>
<Input>.so***** @club.org</Input>
<ExpectedOutput >false</ExpectedOutput>
</Case>
</TestList>
<Regexp>^(\w([\.\-\w]*\w)?)@(\w([\.\-\w]*\w)*\.\w([\.\-\w]*\w)?)$</Regexp>
</Email.Validatio n.Input>
"Alan Pretre" <no@spam> wrote in message
news:ep******** ******@TK2MSFTN GP09.phx.gbl...
Can anyone help me figure out a regex pattern for the following input
example:
xxx:a=b,c=d,yyy :e=f,zzz:www:g= h,i=j,l=m
I would want four matches from this:
1. xxx a=b,c=d
2. yyy e=f
3. zzz (empty)
4. www g=h,i=j,l=m
None of the letters here are single letters, but rather placeholders for
arbitrary words. For example,
LTG:LTG=2-41-53-57,JOB:JN=113&& 116&125&&127,CP T:CODE=09789,TR ATYP=AMBINC-7-A MBINC/CPTGRP-0-CPTGRP
Would result in:
1. LTG LTG=2-41-53-57
2. JOB JN=113&&116&125 &&127
3. CPT CODE=09789,TRAT YP=AMBINC-7-AMBINC/CPTGRP-0-CPTGRP
Everything I've come up with so far would require me to iterate over
substrings. It'd be nice to have just a single matching operation. TIA.
-- Alan