使用模板解析句子 [英] Parsing sentence using template

查看:73
本文介绍了使用模板解析句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,有一个句子模板:

告诉我@city的预测

和句子:

告诉我纽约的预告

我怎样才能获得城市名称(纽约)模板?或者,例如:

告诉我@dity的预测@date - > 告诉我明天拉斯维加斯的预告

如何才能获得拉斯维加斯明天

解决方案

基本上你可以用 \s +(?< city> [\\\] +}取代关键字@city? )\ s +



示例:

  string  template1 =  @  ^告诉我\ + +的预测(?< ;城市> [\S\s] +)\s + for\s +(小于?日期和GT; [\S\s] +)

;





在C#中你使用

匹配m = Regex.Match( 告诉我明天拉斯维加斯的预测,template1); 
if (m.Success)
{
string city = m.Groups [ city]。
string date = m.Groups [ 日期]值。
}





剩下的你可以自己做。

这里有一些关于常规的信息表达式 http://www.regular-expressions.info/ [ ^ ]


For example, there is a sentence template:
"Tell me the forcast of @city"
And the sentence:
"Tell me the forcast of New York"
How I can get the city name (New York) using the template? Or, for example:
"Tell me the forcast of @city for @date" -> "Tell me the forcast of Las Vegas for tomorrow"
How I can get Las Vegas and tomorrow?

解决方案

Basically you can replace the keyword @city with \s+(?<city>[\S\s]+?)\s+

Example:

string template1 = @"^Tell me the forcast of\s+(?<city>[\S\s]+?)\s+for\s+(?<date>[\S\s]+)


";



In C# you use

Match m = Regex.Match("Tell me the forcast of Las Vegas for tomorrow", template1);
if (m.Success)
{
    string city = m.Groups["city"].Value;
    string date = m.Groups["date"].Value;
}



The rest you can do by yourself.
Here is some info about Regular Expressions http://www.regular-expressions.info/[^]


这篇关于使用模板解析句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆