Regular Expression Question

N

Nightcrawler

I have a regular expression that catches these patterns:

value1 - value2
value1 - value2 (value3)
value1 - value2 [value3]
value1 - value2 "value3"

As you can see value3 is always optional. The regular expression I use
is:

(?<Value1>.+)-(?:(?<Value2>.+)??(?<Value3>(?:\([^\)]+\)|\[[^\]]+\]|\"[^
\"]+\"))|(?<Value2>.+))

I am trying to expand this regular expression to also catch this
pattern:

value1 - value2 - value3
value1 - value2 -(value3)
value1 - value2 -[value3]
value1 - value2 -"value3"

Can anyone please help me out with this. I have been trying all
weekend and I can't figure it out.

Much appreciated.

Thanks
 
M

Martin Koch

Hi

here one possible method of resolution



string s1 = "value1 - value2";
string s2 = "value1 - value2 (value3)";
string s3 = "value1 - value2 \"value3\"";
string s4 = "value1 - value2 [value3]";
string vs;

Regex re = new Regex( @"(?<v1>.+)\s+-(?<v2>.+)\s+(?<v3>.+)$" );

Match ma = re.Match( s1 );
vs = ma.Groups["v1"].Value + " " + ma.Groups["v2"].Value + "
" + ma.Groups["v3"].Value + "\r\n";

ma = re.Match( s2 );
vs += ma.Groups["v1"].Value + " " + ma.Groups["v2"].Value + "
" + ma.Groups["v3"].Value + "\r\n";

ma = re.Match( s3 );
vs += ma.Groups["v1"].Value + " " + ma.Groups["v2"].Value + "
" + ma.Groups["v3"].Value + "\r\n";

ma = re.Match( s4 );
vs += ma.Groups["v1"].Value + " " + ma.Groups["v2"].Value + "
" + ma.Groups["v3"].Value + "\r\n";

Console.WriteLine( vs );
Console.ReadLine();
 
M

Martin Koch

Hi
One possible method of resolution

string s1 = "value1 - value2";
string s2 = "value1 - value2 (value3)";
string s3 = "value1 - value2 \"value3\"";
string s4 = "value1 - value2 [value3]";
string vs;

Regex re = new Regex( @"(?<v1>.+)\s+-\s+(?<v2>.+)\s*(?<v3>.+)*" );

Match ma = re.Match( s1 );
vs = ma.Groups["v1"].Value + " " + ma.Groups["v2"].Value + " " +
ma.Groups["v3"].Value + "\r\n";

ma = re.Match( s2 );
vs += ma.Groups["v1"].Value + " " + ma.Groups["v2"].Value + " " +
ma.Groups["v3"].Value + "\r\n";

ma = re.Match( s3 );
vs += ma.Groups["v1"].Value + " " + ma.Groups["v2"].Value + " " +
ma.Groups["v3"].Value + "\r\n";

ma = re.Match( s4 );
vs += ma.Groups["v1"].Value + " " + ma.Groups["v2"].Value + " " +
ma.Groups["v3"].Value + "\r\n";

Console.WriteLine( vs );

Greetings
 
M

Martin Koch

Hi
I think now I have it !!!

string s1 = "value1 - value2";
string s2 = "value1 - value2 (value3)";
string s3 = "value1 - value2 \"value3\"";
string s4 = "value1 - value2 [value3]";
string vs;

Regex re = new Regex(
@"(?<v1>.+)\s+-\s+(?(?=.+\s)(?<v2>.+)\s[\(\[""](?<v3>.+)[\)\]""]|(?<v2>.+))"
);

Match ma = re.Match( s1 );
vs = "s1: " + ma.Groups["v1"].Value + " ; " +
ma.Groups["v2"].Value + " ; " + ma.Groups["v3"].Value + "\r\n";

ma = re.Match( s2 );
vs += "s2: " + ma.Groups["v1"].Value + " ; " +
ma.Groups["v2"].Value + " ; " + ma.Groups["v3"].Value + "\r\n";

ma = re.Match( s3 );
vs += "s3: " + ma.Groups["v1"].Value + " ; " +
ma.Groups["v2"].Value + " ; " + ma.Groups["v3"].Value + "\r\n";

ma = re.Match( s4 );
vs += "s4: " + ma.Groups["v1"].Value + " ; " +
ma.Groups["v2"].Value + " ; " + ma.Groups["v3"].Value + "\r\n";

Console.WriteLine( vs );

// Outputs:
// s1: value1 ; value2 ;
// s2: value1 ; value2 ; value3
// s3: value1 ; value2 ; value3
// s4: value1 ; value2 ; value3
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top