I have a C# application, which I'm using RegEx to run an expect from a Unix response. I currently have this.
//will pick up :
// What is your name?:
// [root@localhost ~]#
// [root@localhost ~]$
// Do you want to continue [y/N]
// Do you want to continue [Y/n]
const string Command_Prompt_Only = @"[$#]|\[.*@(.*?)\][$%#]";
const string Command_Question_Only = @".*\?:|.*\[y/N\]/g";
const string Command_Prompt_Question = Command_Question_Only + "|" + Command_Prompt_Only;
This works as I've tested it with www.regexpal.com, but I think I need some optimization as there are times, it seems to slow way down when I use Command_Prompt_Question.
var promptRegex = new Regex(Command_Prompt_Question);
var output = _shellStream.Expect(promptRegex, timeOut);
I might want to mention I'm using SSH.NET to talk to these Linux servers, but I don't think it's a SSH.NET issue because when I use Command_Prompt_Only it's fast.
Does anyone see any issues with the const string I'm using? Is there a better way to do it?
My project is open source if you feel like you want to go play with it.
https://github.com/gavin1970/Linux-Commander
Code in question: https://github.com/gavin1970/Linux-Commander/blob/master/Linux-Commander/common/Ssh.cs
It's call Linux Commander and I'm attempting to build a virtual linux console with Ansible support.
static readonly Regex
) withRegexOptions.Compiled
? I note that your regex is using ECMAScript syntax/g
which is not supported by .NET - you also aren't putting each sub-expression in a non-capturing group - is that intentional?const
with reference-types likeRegex
, you can only usestatic readonly
. Saying "const is faster" is an oversimplification:const
andstatic readonly
have different semantics and the C# compiler will inlineconst
values even across assembly boundaries (which can be a source of bugs if you don't rebuild when updating assembly dependencies)./g
in ECMAScript regex is the same as enumerating allMatch
values in theMatchCollection
- however your code doesn't seem to evaluate the regex by itself, instead it's done by the.Expect
method. Where is that defined and what does it do?