Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?
Posted
Comments
[no name] 1-Nov-14 9:34am    
By writing a class which manages the parameters....?
BillWoodruff 1-Nov-14 10:10am    
Please clarify: is it always the case that the string you are parsing for the 'City is always going to have the sequence "Tell me the forecast of;" or, is it the case that you also want to handle parsing other sentences like: "I was looking at the forecast for Los Angeles." By the way, "forcast" is spelled "forecast."
Arlert 1-Nov-14 10:13am    
Sorry, I am not an english-speaking guy.
I have a list of sentences, from whici i will get the most appropriate sentence.
For example: "Tell me the forecast of @city", "Tell me the forecast of @city for @date", "What is the weather in @city" etc.

1 solution

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

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


In C# you use
C#
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/[^]
 
Share this answer
 
v3
Comments
BillWoodruff 1-Nov-14 9:54am    
+5 very nice !
George Jonsson 1-Nov-14 10:54am    
Thanks Bill.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900