MS Visual Studio 2010のC#。コード検証の一部として出力をログファイルに書き込みます。デバッガは、Match出力と同様に、グループが適切に一致していることを示します。唯一間違っているのは出力ファイルで、私はその理由を知らない。ここでは、ファイルから取得した出力を示します。readlineは入力行、MatchはRegexの一致結果、Slot/PortはRegexの名前付きグループから値を取得した結果です。ログからRegex Matchが入力行と一致することがわかります。デバッガは、名前付きグループが機能していると言います。ログファイルの出力がデバッガと一致しない
readline is: fc1/1 is up
Match is: fc1/1 is
Slot and Port are: 1/1
readline is: fc1/3 is up
Match is: fc1/3 is
Slot and Port are: 1/1
ここでは一致するコードを示します。デバッガでの表示を簡単にするために追加したものがいくつかあります。
if (RegexExtensions.TryMatch(out InterfaceMatch, trimmed, InterfaceMatchString))
{
SlotNum = Convert.ToInt32(InterfaceMatch.Groups["slot"].Value);
PortNum = Convert.ToInt32(InterfaceMatch.Groups["port"].Value);
string slot = InterfaceMatch.Groups["slot"].Value;
string port = InterfaceMatch.Groups["port"].Value;
// write the value of current incoming readline
CiscoDecodeVariables.swlog.WriteLine(String.Format("readline is: {0}", readline));
// write the value of the current match
CiscoDecodeVariables.swlog.WriteLine(String.Format("Match is: {0}", InterfaceMatch.Value.ToString()));
string slotasstring = SlotNum.ToString();
string portasstring = PortNum.ToString();
// here is the line that writes what the slot and port values are
CiscoDecodeVariables.swlog.WriteLine(String.Format("Slot and Port are: {0}/{0}", slotasstring, portasstring));
CiscoDecodeVariables.swlog.Flush();
//sw.Close();
currPort = ((PortModule)ModuleList[SlotNum]).FCPortList[PortNum - 1];
}
最後に、ここにStreamWriterのコードを示します。私は静的な変数で静的なクラスをdeclearedので、私は必要なStreamwriterを使用することができます。 文字列InterfaceMatchString = @ "(?\ D +)FC(?\ D +)が/である" 問題の正規表現のマッチパターンを追加
static public class CiscoDecodeVariables
{
static public string LogFileOutPutPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\\ciscodecodelog.txt";
static public StreamWriter swlog = new StreamWriter(LogFileOutPutPath);
}
。
また、RegexExtensions.TryMatch()は、成功した一致に対してtrueを返し、InterfaceMatchインスタンスを結果に設定する静的メソッドです。
これは、あなたがマッチを書き留めている行を止め、ウォッチウィンドウに 'InterfaceMatch.Value.ToString()'を置くと、swlogに書き込まれないものが出てくるということですあなたが期待するように?もしそうなら、それは変だから... – Chris
はい。デバッガはよく見えます。上記の 'currPort'行は現在のポートを正しいポートに割り当てます。つまり、正規表現から解析したものと一致するスロットとポートを持つモジュールリストのポートです。私が考えることができるのはString.Format()がスロットとポートの値を使って何かをしているということだけです。 –
Regex式と入力文字列を追加してください。ありがとう!また、SlotNumとPortNumはどんなタイプですか? –