ユーザー入力を格納する文字列の配列があり、入力ユーザーに特定の単語のみが含まれているかどうかを確認したいのですがEND
、単語の前または単語の後にスペースがあるかどうかは気にしません。ユーザーは、 END
「END」、「END」、「END」などの単語を入力できます。END
入力文字列にスペースを考慮せずに単語のみが含まれていることを確認したいだけです。
私は試した
Regex regex_ending_char = new Regex(@"^END|^\s+END$|^END+\s$");
// to compare the word "END" only nothing before it nor after it -
// space is of anywhere before or after the word
Match Char_Instruction_match = regex_ending_char.Match(Instruction_Separator[0]);
if (!Char_Instruction_match.Success) // True if word doesn't end with "END"
{
richTextBox2.Text += "Error in line " + (LineNumber + 1) + ", Code must end with 'END'" + Environment.NewLine;
}`
私も試しました
Regex regex_ending_char = new Regex(@"^END|^\s+END$|^END+\s$");
// to compare the word "END" only nothing before it nor after
// it - space is of anywhere before or after the word
Regex.Replace(Instruction_Separator[0], @"\s+", "");
Match Char_Instruction_match = regex_ending_char.Match(Instruction_Separator[0]);
if (!Char_Instruction_match.Success) // True if word doesn't end with "END"
{
richTextBox2.Text += "Error in line " + (LineNumber + 1) + ", Code must end with 'END'" + Environment.NewLine;
}`
問題は、配列の最初の要素のみをチェックする必要があり、Instruction_Separator[0]
他の要素をチェックする必要がないことです。したがって、ユーザーがEND
「END」のように単語の前にスペースを入力すると、Instruction_Separator
配列は次のようになるInstruction_Separator[0] = " ", Instruction_Separator[1] = END
ため、ユーザーが正しい文字列を入力しても、コードはif条件に進みます。最初にスペースを入力しただけで、問題はありません。単語の前後にスペースがある場合は with 。
皆さん、ご回答ありがとうございます。すべての回答を尊重します。私がやろうとしているのは、アセンブラを構築することです。構文エラーをチェックする必要があり、コメントはユーザー入力で問題ありません。SO たとえば、ユーザー入力が次のような場合:
ORG 100 //Begin at memory location 100
LDA A // Load A
A, DEC 83 // A has a decimal value of 83
END // End the code
構文エラーはなく、結果を出すことができました。
また、ユーザーが各行の前にスペースを追加した場合も問題ありません
ORG 100 //Begin at memory location 100
LDA A // Load A
A, DEC 83 // A has a decimal value of 83
END // End the code
そのため、各行に正しい構文が含まれているかどうかを確認したいのですが、各行の正しい形式の前後のスペースはあまり気にしません。
ユーザー構文エラーは次のようになります。
OR G 100 //Begin at memory location 100
LDA A // Load A
A, DEC 83 // A has a decimal value of 83
EN To end the code
ORG が「OR G」と書かれていることに注意してください。これは間違っています。END も「EN」と書かれており、ユーザーはコメント「End the code」の前に「//」を配置するのを忘れていました。
だから私がする必要があるのは、最後の行に「END」という単語が含まれていることを確認することです。「//」がある場合は、その後にコメントがあります。ただし、行にコメントを追加する場合は、「//」と入力する必要があります。彼がコメントを追加したくない場合、それは必須ではありません. 上記のように正規表現を使用してこれを行う方法を考えてみRegex regex_ending_char = new Regex(@"^END|^\s+END$|^END+\s$");
ましたが、正しく動作していないようです
返信をお待ちしております。