How to use regular expressions for expected string - wiwanek/Expect.NET GitHub Wiki

Introduction

Typically to wait for expected output is used method:

void Session.Expect(string query, ExpectedHandler expectedHandler)

or one of its variant.

This method checks if query is substring of received output string. If so expectedHandler is executed. It's described in following article: How to react to session's output

Some outputs may include parts which change dynamically - for instance timestamps. The most convenient way to match such string is to use regular expressions.

How to use regular expressions

Expect for .NET supports regular expressions matching from version 2.1.0. There are set of Session.Expect methods supporting System.Text.RegularExpressions.Regex object as an argument.

void Expect(Regex regex, ExpectedHandler handler)

regex object representing regular expression handler defines actions to be performed when expected output is found

Usage example:

Session session; // session should be created here...
session.Expect(new Regex("exp.*ring"), () => Console.WriteLine("FOUND IT!")); 
// if regular expression "exp.*ring" matches session output, 
// then print "FOUND IT!" to standard output

async Task ExpectAsync(Regex regex, ExpectedHandler handler)

regex object representing regular expression handler defines actions to be performed when expected output is found

Asynchronous version of void Expect(Regex regex, ExpectedHandler handler).

Usage example:

Session session; // session should be created here...
await session.ExpectAsync(new Regex("exp.*ring"), () => Console.WriteLine("FOUND IT!")); 
// if regular expression "exp.*ring" matches session output, 
// then print "FOUND IT!" to standard output

void Expect(Regex regex, ExpectedHandlerWithOutput handler)

regex object representing regular expression handler defines actions to be performed when expected output is found, receives as parameter session output from time when method was called up to expected output is found

Usage example:

Session session; // session should be created here...
session.Expect("exp.*ring", (s) => Console.WriteLine(s)); 
// if regular expression "exp.*ring" matches session output, 
// then print session output to standard output

async Task ExpectAsync(Regex regex, ExpectedHandlerWithOutput handler)

regex object representing regular expression handler defines actions to be performed when expected output is found, receives as parameter session output from time when method was called up to expected output is found

Asynchronous version of void Expect(Regex regex, ExpectedHandler handler).

Usage example:

Session session; // session should be created here...
await session.ExpectAsync("exp.*ring", (s) => Console.WriteLine(s)); 
// if regular expression "exp.*ring" matches session output, 
// then print session output to standard output