私は正規表現で行きます:
最初の例、もしあなたがStatus: ZZZZZ
-のようなメッセージしか持っていなかったら:
String status = Regex.Match(@"(?<=Status: ).*");
// Explanation of "(?<=Status: ).*" :
// (?<= Start of the positive look-behind group: it means that the
// following text is required but won't appear in the returned string
// Status: The text defining the email string format
// ) End of the positive look-behind group
// .* Matches any character
Status: ZZZZZ
とAction: ZZZZZ
-のようなメッセージしかない場合の2番目の例:
String status = Regex.Match(@"(?<=(Status|Action): ).*");
// We added (Status|Action) that allows the positive look-behind text to be
// either 'Status: ', or 'Action: '
ここで、ユーザーに独自の形式を提供する可能性を与えたい場合は、次のようなものを思い付くことができます。
String userEntry = GetUserEntry(); // Get the text submitted by the user
String userFormatText = Regex.Escape(userEntry);
String status = Regex.Match(@"(?<=" + userFormatText + ").*");
これにより、ユーザーは、、、、、などの形式を送信Status:
できAction:
ますThis is my friggin format, now please read the status -->
。
この部分は、ユーザーが、、、...などのRegex.Escape(userEntry)
特殊文字を送信して正規表現を壊さないようにするために重要です。\
?
*
ユーザーがフォーマットテキストの前または後にステータス値を送信するかどうかを知るには、いくつかの解決策があります。
ユーザーにステータス値がどこにあるかを尋ねて、それに応じて正規表現を作成できます。
if (statusValueIsAfter) {
// Example: "Status: Closed"
regexPattern = @"(?<=Status: ).*";
} else {
// Example: "Closed:Status"
regexPattern = @".*(?=:Status)"; // We use here a positive look-AHEAD
}
または、より賢く、ユーザーエントリにタグのシステムを導入することもできます。たとえば、ユーザーが送信するStatus: <value>
か<value>=The status
、タグ文字列を置き換えることで正規表現を作成します。