0 votes
by (370 points)
edited

Hello,

I have some code that creates a virtualterminal (vt), and I use vt.Expect(string, wait_time) to look for server responses.

In testing my application, I use a server response string that looks like this:

string serverResponse = "Misc~MsgId=10060~Action=QueueEntry~Seq=765~Queue=2~Pri=1~Date=10/27/13~Time=01:09:19~Running=1~Cancel=0~Stat=Running~Job=REPORTLIST.JOB~AfterDate=~AfterTime=";

If I use this code:

vt.Expect(serverResponse, 1000)

Expect catches this, as expected. However this string is going to vary, so I created this regular expression pattern to capture input (and used this code to test it):

matchPattern = string.Format("Seq=([0-9])~Queue=[0-9]~Pri=[0-9]~Date=[0-9]/[0-9]/[0-9]~Time=[0-9]:[0-9]:[0-9]~Running=[0-9]~Cancel=[0-9]~Stat=[A-Za-z]~Job=", jobName);
Match match = Regex.Match(serverResponse, matchPattern);
if (match.Success)
{
seqNumber = int.Parse(match.Groups[1].Value.Trim()); string writeln = string.Format("Job Started ! name: {0} Seq: {1}", jobName, seqNumber);
Console.WriteLine(writeln); }
else
{
Console.WriteLine("Did not match string: " + serverResponse);
}

And this code catches the regular expression. But when I try this:

if (vt.Expect(matchPattern, 1000)) { # etc }

This DOES NOT catch the pattern. I thought I could just replace the exact string with a patterned string and the Expect method would parse it correctly.

Please tell me what I am missing.

Thanks !

Chew

4 Answers

0 votes
by (70.2k points)
edited

You have to create Regex object and pass it to the Expect method like this:

if (vt.Expect(new Regex(matchPattern), 1000)) { # etc }
0 votes
by (370 points)
edited

When I do this, I get a compile error that says, "Cannot implicitly convert type 'System.Text.RegularExpressions.Match' to 'bool' on this line:

if (vt.Expect(new Regex(matchPattern), 5000))

Here's my code as compiled:

string matchPattern = string.Format("Seq=[0-9]~Queue=[0-9]~Pri=[0-9]~Date=[0-9]/[0-9]/[0-9]~Time=[0-9]:[0-9]:[0-9]~Running=[0-9]~Cancel=[0-9]~Stat=[A-Za-z]~Job=", jobName);

if (vt.Expect(new Regex(matchPattern), 5000))
{
matchPattern = string.Format("Seq=([0-9])~Queue=[0-9]~Pri=[0-9]~Date=[0-9]/[0-9]/[0-9]~Time=[0-9]:[0-9]:[0-9]~Running=[0-9]~Cancel=[0-9]~Stat=[A-Za-z]~Job=", jobName);
Match match = Regex.Match(response, matchPattern);
if (match.Success)
{
seqNumber = int.Parse(match.Groups[1].Value.Trim());
string writeln = string.Format("Job Started ! name: {0} Seq: {1}", jobName, seqNumber);
Console.WriteLine(writeln);
}
else
{
Console.WriteLine("Did not match string: " + m_ServerResponse);
}
}

FYI, I do a second pattern match b/c I want to grab the Seq (number) for use later in my app.

I've also tried this, but get the same compile-time error:

Regex mtch = new Regex(matchPattern);
if (vt.Expect(mtch, 5000))
{
//string matchPattern = string.Empty;
matchPattern = string.Format("Seq=([0-9])~Queue=[0-9]~Pri=[0-9]~Date=[0-9]/[0-9]/[0-9]~Time=[0-9]:[0-9]:[0-9]~Running=[0-9]~Cancel=[0-9]~Stat=[A-Za-z]~Job=", jobName);
Regex match = Regex.Match(response, matchPattern);
if (match.Success)
{
seqNumber = int.Parse(match.Groups[1].Value.Trim());
string writeln = string.Format("Job Started ! name: {0} Seq: {1}", jobName, seqNumber);
Console.WriteLine(writeln);
}
else
{
Console.WriteLine("Did not match string: " + m_ServerResponse);
}
}

Additionally, I've tried:

bool matched = vt.Expect(new Regex(matchPattern), 5000);

with the same compile error.

However, the following stand-alone code DOES NOT throw an error:

vt.Expect(new Regex(matchPattern), 5000);

But this line isn't useful.

HTH and thanks for the quick replies. This one might be more difficult to solve, as this is a C# error, not a Rebex thing.

Chew

0 votes
by (370 points)
edited

OK, I was able to solve this problem. The thing I didn't notice is that if you pass a string to Expect, it returns a bool. BUT, if you pass a Regex object to the Expect, it returns a Match. So the following code fixes this:

Match match = vt.Expect(new Regex(matchPattern), 5000);
if (match.Success)
{
// etc
}

HTH

Chew

by (70.2k points)
edited

Yes. I am sorry, it was my mistake. Your solution is correct.

The advantage of returning Match instead of bool is, that you give additional information such as length of the match etc.

0 votes
by (370 points)
edited

In my code sample above, I needed access to the match object later in my code. But if you didn't need this access, the above could also be written as:

if (vt.Expect(new Regex(matchPattern, RegexOptions.IgnoreCase), 2000).Success)
{
// etc
}

HTH

Chew

...