私は正規表現となるだろう
:あなただけ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
持っていた場合 - メッセージのように:
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)
部分は、ユーザーが\
、?
、*
などの特殊文字を提出することによって、あなたの正規表現を壊さないようにすることが重要である...
ユーザーは、形式のテキストの前または後にステータス値を送信する場合、あなたはいくつかのソリューションを持って知っている
:
あなたは彼のステータス値があることをユーザーに尋ね、その後、あなたはそれに応じてregexで構築することができ:
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
を送信し、タグ文字列を置き換えて正規表現を作成します。
具体的にしてください。あなたが試したコードを入力データ形式の例で表示し、あなたの期待することを質問に書いてください。いいえ...いいえ、私は答えます - RegExpを学んでください:) – Reniuz