检查字符串的语法-C#
我试图找出如何分析C#中句子的语法。 在我的例子中,我有一个每个句子都必须遵循的语法。 语法如下所示: A‘B’是A‘C’ 每个句子必须包含五个单词。我句子的第一个单词必须是“A”,第三个是“is”,第四个是“A” 现在,我想检查一个测试句子是否符合我的语法 测试句: 狗不是猫 在这个例子中,测试句子是错误的,因为第四个单词是“no”,而不是“a”,它应该基于语法 我读过关于LINQ的文章,在那里我可以查询包含一组特定单词的句子 代码如下所示:检查字符串的语法-C#,c#,string,linq,string-comparison,C#,String,Linq,String Comparison,我试图找出如何分析C#中句子的语法。 在我的例子中,我有一个每个句子都必须遵循的语法。 语法如下所示: A‘B’是A‘C’ 每个句子必须包含五个单词。我句子的第一个单词必须是“A”,第三个是“is”,第四个是“A” 现在,我想检查一个测试句子是否符合我的语法 测试句: 狗不是猫 在这个例子中,测试句子是错误的,因为第四个单词是“no”,而不是“a”,它应该基于语法 我读过关于LINQ的文章,在那里我可以查询包含一组特定单词的句子 代码如下所示: //Notice the third senten
//Notice the third sentence would have the correct syntax
string text = "A Dog is no Cat. My Dog is a Cat. A Dog is a Cat.";
//Splitting text into single sentences
string[] sentences = text.Split(new char[] { '.'});
//Defining the search terms
string[] wordToMatch ={"A", "is"};
//Find sentences that contain all terms I'm looking for
var sentenceQuery = from sentence in sentences
let w = sentence.Split(new Char[] {'.'})
where w.Distinct().Intersect(wordsToMatch).Count == wordsToMatch.Count()
select sentence;
using System.Text.RegularExpressions;
...
string source = "A Dog is no Cat.";
bool result = Regex.IsMatch(source, @"^A\s+[A-Za-z0-9]+\s+is\s+a\s+[A-Za-z0-9]+\.$");
通过这段代码,我可以检查句子是否包含我要查找的术语,但问题是它没有检查单词在句子中的位置。
有没有一种方法可以同时检查位置,或者有更好的方法来检查带有C#的句子的语法?尝试使用正则表达式,比如:
//Notice the third sentence would have the correct syntax
string text = "A Dog is no Cat. My Dog is a Cat. A Dog is a Cat.";
//Splitting text into single sentences
string[] sentences = text.Split(new char[] { '.'});
//Defining the search terms
string[] wordToMatch ={"A", "is"};
//Find sentences that contain all terms I'm looking for
var sentenceQuery = from sentence in sentences
let w = sentence.Split(new Char[] {'.'})
where w.Distinct().Intersect(wordsToMatch).Count == wordsToMatch.Count()
select sentence;
using System.Text.RegularExpressions;
...
string source = "A Dog is no Cat.";
bool result = Regex.IsMatch(source, @"^A\s+[A-Za-z0-9]+\s+is\s+a\s+[A-Za-z0-9]+\.$");
模式说明:
^ - start of the string (anchor)
A - Letter A
\s+ - one or more whitelines (spaces)
[A-Za-z0-9]+ - 1st word (can contain A..Z, a..z letters and 0..9 digits)
\s+ - one or more whitelines (spaces)
is - is
\s+ - one or more whitelines (spaces)
a - a
\s+ - one or more whitelines (spaces)
[A-Za-z0-9]+ - 2nd word (can contain A..Z, a..z letters and 0..9 digits)
\. - full stop
$ - end of the string (anchor)
您可以稍微修改代码并获得实际的第一个和第二个字符串的值:
string source = "A Dog is a Cat."; // valid string
string pattern =
@"^A\s+(?<First>[A-Za-z0-9]+)\s+is\s+a\s+(?<Second>[A-Za-z0-9]+)\.$";
var match = Regex.Match(source, pattern);
if (match.Success) {
string first = match.Groups["First"].Value; // "Dog"
string second = match.Groups["Second"].Value; // "Cat"
...
}
string source=“狗是猫。”;//有效字符串
字符串模式=
@“^A\s+(?[A-Za-z0-9]+)\s+是\s+A\s+(?[A-Za-z0-9]+)\。$”;
var match=Regex.match(源、模式);
如果(匹配成功){
string first=match.Groups[“first”].Value;/“Dog”
string second=match.Groups[“second”].Value;/“Cat”
...
}
尝试使用正则表达式,如下所示:
//Notice the third sentence would have the correct syntax
string text = "A Dog is no Cat. My Dog is a Cat. A Dog is a Cat.";
//Splitting text into single sentences
string[] sentences = text.Split(new char[] { '.'});
//Defining the search terms
string[] wordToMatch ={"A", "is"};
//Find sentences that contain all terms I'm looking for
var sentenceQuery = from sentence in sentences
let w = sentence.Split(new Char[] {'.'})
where w.Distinct().Intersect(wordsToMatch).Count == wordsToMatch.Count()
select sentence;
using System.Text.RegularExpressions;
...
string source = "A Dog is no Cat.";
bool result = Regex.IsMatch(source, @"^A\s+[A-Za-z0-9]+\s+is\s+a\s+[A-Za-z0-9]+\.$");
模式说明:
^ - start of the string (anchor)
A - Letter A
\s+ - one or more whitelines (spaces)
[A-Za-z0-9]+ - 1st word (can contain A..Z, a..z letters and 0..9 digits)
\s+ - one or more whitelines (spaces)
is - is
\s+ - one or more whitelines (spaces)
a - a
\s+ - one or more whitelines (spaces)
[A-Za-z0-9]+ - 2nd word (can contain A..Z, a..z letters and 0..9 digits)
\. - full stop
$ - end of the string (anchor)
您可以稍微修改代码并获得实际的第一个和第二个字符串的值:
string source = "A Dog is a Cat."; // valid string
string pattern =
@"^A\s+(?<First>[A-Za-z0-9]+)\s+is\s+a\s+(?<Second>[A-Za-z0-9]+)\.$";
var match = Regex.Match(source, pattern);
if (match.Success) {
string first = match.Groups["First"].Value; // "Dog"
string second = match.Groups["Second"].Value; // "Cat"
...
}
string source=“狗是猫。”;//有效字符串
字符串模式=
@“^A\s+(?[A-Za-z0-9]+)\s+是\s+A\s+(?[A-Za-z0-9]+)\。$”;
var match=Regex.match(源、模式);
如果(匹配成功){
string first=match.Groups[“first”].Value;/“Dog”
string second=match.Groups[“second”].Value;/“Cat”
...
}
正则表达式适用于此,它是最简洁的解决方案,但可能不是最可读的解决方案。下面是一个简单的方法,如果句子有效,它将返回true:
private bool IsSentenceValid(string sentence)
{
// split the sentence into an array of words
char[] splitOn = new char[] {' '};
string[] words = sentence.ToLower().Split(splitOn); // make all chars lowercase for easy comparison
// check for 5 words.
if (words.Length != 5)
return false;
// check for required words
if (words[0] != "a" || words[2] != "is" || words[3] != "a")
return false;
// if we got here, we're fine!
return true;
}
正则表达式可以做到这一点,它是最简洁的解决方案,但可能不是最可读的解决方案。下面是一个简单的方法,如果句子有效,它将返回true:
private bool IsSentenceValid(string sentence)
{
// split the sentence into an array of words
char[] splitOn = new char[] {' '};
string[] words = sentence.ToLower().Split(splitOn); // make all chars lowercase for easy comparison
// check for 5 words.
if (words.Length != 5)
return false;
// check for required words
if (words[0] != "a" || words[2] != "is" || words[3] != "a")
return false;
// if we got here, we're fine!
return true;
}
只是想抛砖引玉。我将为此编写三节课:
SentenceManager
:它获取字符串作为句子,并具有公共方法公共字符串GetWord(word\u index)
。例如,GetWord(3)
将返回已给定给类构造函数的句子中的第三个单词
SentenceSyntax
:在这门课上,你可以说你的句子必须有多少个单词。必须知道哪些单词,您也可以设置这些单词的索引
SyntaxChecker
:此类获取一个SentenceSyntax
对象和一个SentenceManager
对象,并具有一个名为Check
的函数,如果语法与句子匹配,该函数将返回true
记住,有成千上万种方法可以让事情成功。但是有几种方法可以把它做好。只是想抛砖引玉。我将为此编写三节课:
SentenceManager
:它获取字符串作为句子,并具有公共方法公共字符串GetWord(word\u index)
。例如,GetWord(3)
将返回已给定给类构造函数的句子中的第三个单词
SentenceSyntax
:在这门课上,你可以说你的句子必须有多少个单词。必须知道哪些单词,您也可以设置这些单词的索引
SyntaxChecker
:此类获取一个SentenceSyntax
对象和一个SentenceManager
对象,并具有一个名为Check
的函数,如果语法与句子匹配,该函数将返回true
记住,有成千上万种方法可以让事情成功。但是有几种方法可以做到正确。您肯定应该使用Regex或类似的工具来实现这一点
只是为了好玩,我想按你的方式做。如果我发疯了,我会这样做:)
使用这段代码,您还可以获得灵活性,例如不区分大小写的比较,以及在单词周围修剪空格等-当然,您必须为此编写代码您肯定应该使用Regex或类似的工具来完成这项工作
只是为了好玩,我想按你的方式做。如果我发疯了,我会这样做:)
使用此代码,您还可以获得灵活性,例如不区分大小写的比较,以及在单词周围修剪空格等-当然,您必须进行编码,因为“intersect”没有说明单词在列表中的位置,因此这不起作用。您要做的是拆分输入字符串并编写一个与模式匹配的方法——或者通过硬编码if(words[0]!=“a”)返回false代码>等等,或者给它一组规则——可能是一个对象列表,比如{int Index;string RequiredWord;}
。你的第一句话令人震惊,但你的实际要求是相当温和和合理的。更新如果我有更多的理智,我会直接去德米特里的正则表达式的想法。你的代码也可以构建正则表达式字符串(那么你有3个问题)是“Dog42是Cat17”有效语法吗?@AleksAndreev在本例中是的。我不想检查这个句子是否有意义。只是想知道这个句子是否包含我指定的语法“intersect”并没有说明单词在列表中的位置,所以这不起作用。您要做的是拆分输入字符串并编写一个与模式匹配的方法——或者通过硬编码if(words[0]!=“a”)返回false代码>等,或者给它一组ru