Joe*_*man 1 c# linq string string-comparison
我试图找出如何分析C#中句子的语法.在我的情况下,我有一个句法,每个句子必须遵循.语法如下所示:
'B'是'C'.
每个句子都必须包含五个单词.我的句子的第一个词必须是'A',第三个'是'和第四个'a'.
现在我想检查一个测试句子,如果它符合我的语法.
测试句子:
狗不是猫.
在这个例子中,测试句子是错误的,因为第四个单词是'no'而不是'a'它应该基于语法.
我读了LINQ,我可以查询包含一组指定单词的句子.
代码看起来像这样:
//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;
Run Code Online (Sandbox Code Playgroud)
使用此代码,我可以检查句子是否包含我正在寻找的术语,但问题是它不是检查句子中单词的位置.有没有办法可以检查位置,或者用C#检查句子语法的更好方法?
尝试使用正则表达式,如下所示:
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]+\.$");
Run Code Online (Sandbox Code Playgroud)
模式说明:
^ - 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)
Run Code Online (Sandbox Code Playgroud)
您可以稍微修改代码并获取实际的第1和第2个字符串的值:
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"
...
}
Run Code Online (Sandbox Code Playgroud)