Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c语言的grammerbuilder中使用正则表达式#_C#_Regex - Fatal编程技术网

C# 在c语言的grammerbuilder中使用正则表达式#

C# 在c语言的grammerbuilder中使用正则表达式#,c#,regex,C#,Regex,我正在使用c#中的语音识别器。我想在grammerbuilder中使用正则表达式,这是我的代码。此代码不起作用。我想传递任何句子示例,但必须传递“This is stackoverflow”或任何一个或多个句子 Regex reg = new Regex(@"[a-z\-A-Z]*"); public Form1(){ InitializeComponent(); try{ SpeechRecognizer spec = new SpeechRecognizer(); Choices c

我正在使用c#中的语音识别器。我想在grammerbuilder中使用正则表达式,这是我的代码。此代码不起作用。我想传递任何句子示例,但必须传递“This is stackoverflow”或任何一个或多个句子

Regex reg = new Regex(@"[a-z\-A-Z]*");
public Form1(){
InitializeComponent();    
try{
SpeechRecognizer spec = new SpeechRecognizer();
Choices chose = new Choices();
chose.Add(reg.ToString());
GrammarBuilder gb = new GrammarBuilder();
gb.Append(chose);
Grammar gm = new Grammar(gb);
spec.LoadGrammar(gm);
spec.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>    (sre_SpeechRecognized);
spec.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(sre_SpeechRejected);
}
Regex reg=newregex(@“[a-z \-a-z]*”);
公共表格1(){
初始化组件();
试一试{
SpeechRecognizer spec=新SpeechRecognizer();
选择的选项=新选项();
选择.Add(reg.ToString());
GrammarBuilder gb=新的GrammarBuilder();
gb.追加(选择);
语法gm=新语法(gb);
规范加载语法(gm);
spec.speechrecogned+=新事件处理程序(sre_speechrecogned);
spec.SpeechRecognitionRejected+=新事件处理程序(sre_SpeechRejected);
}

允许使用一个或多个单词

Regex reg = new Regex(@"^[a-z-A-Z]+(?:\s[a-z-A-Z]+)*$");
我假设这些单词包含字母或
-
连字符


\S+
匹配一个或多个非空格字符。

上述代码是如何工作的?每次ans都是@“[a-z \-a-z]*”
我想传递每个单词。
你所说的单词是什么意思?请尽早解释要求。我已经编辑了上面的问题,我想传递任何句子。你需要使用听写而不是SRGS语法。请参阅
Regex reg = new Regex(@"^\S+(?:\s\S+)*$");