現在、リッチ テキスト ドキュメントから大きな文字列を取得するために、コードで正規表現を使用しています。正規表現は、埋め込まれた画像を見つけて、LinkedResource に変換できるバイト配列に解析します。アプリケーションの RichTextBox から RTF を有効な HTML ドキュメントに変換し、その後、自動的に送信できる MIME エンコード メッセージに変換する必要があります。
正規表現の問題は、画像の文字列セクションが非常に大きいことです。そのため、正規表現は文字列全体の中で多くの可能性に一致させようとしているように感じますが、実際には最初と最後だけを見る必要があります。セクションの終わり。以下の正規表現は、 などのオプション句としてより大きな正規表現に含まれていsomeRegexStringA + "|" + imageRegexString + "|" + "someRegexStringB"
ます。
大量の画像データを解析するときにアプリケーションがフリーズしたように見えないように、大きな文字列内のチェックを少なくするにはどうすればよいですか?
// The Regex itself
private static string imageRegexString = @"(?<imageCheck>\\pict)" // Look for the opening image tag
+ @"(?:\\picwgoal(?<widthNumber>[0-9]+))" // Read the size of the image's width
+ @"(?:\\pichgoal(?<heightNumber>[0-9]+))" // Read the size of the image's height
+ @"(?:\\pngblip(\r|\n))" // The image is the newline after this portion of the opening tag and information
+ @"(?<imageData>(.|\r|\n)+?)" // Read the bitmap
+ @"(?:}+)"; // Look for closing braces
// The expression is compiled so it doesn't take as much time during runtime
private static Regex myRegularExpression = new Regex(imageRegexString, RegexOptions.Compiled);
// Iterate through each image in the document
foreach(Match image in myRegularExpression.Matches(myDocument))
{
// Read the image height and width
int imageWidth = int.Parse(image.Groups["widthNumber"].Value);
int imageHeight = int.Parse(image.Groups["heightNumber"].Value);
// Process the image
ProcessImageData(image.Groups["imageData"].Value);
}