Q:

How to pull distance out of user text input in C#?

I have a search bar visitors use to search for places nearby. It consists of two input boxes: keywords and distance.
I want to simplify it to one box, but allow visitors to enter a distance. They could enter terms like "Costco within 5km" or "Denny's 2mi".
On the server side, I'd like to pull the distance out of that input. I realize there is a lot of room for error. The visitor could put a space after the number (4 km) or may use full text (4 kilometers), or probably any number of other issues to worry about.
If I wanted to offer the ability for visitors to enter (n)km, or (n)mi, what's a good way to parse the data into separate variables?
Let's say a visitor enters "Chinese Indian Korean restaurants 5mi". I'd want to split it into:
string keywords = "Chinese Indian Korean restaurants";
string distance = 5; //(notice no mi, or km)

I'd imagine a regex of some sort would be required, but my regex skills are extremely lacking. Thanks in advance!

A:

Yes, in this case Regular Expressions are your friend. I would focus on matching the distance and removing it from the input text. What remains is the keywords..
Regex distRex = new Regex("(?<dist>\\d+)\\s*(?<unit>mi|km|ft)", RegexOptions.IgnoreCase);

Then you could do this:            
Match m = distRex.Match(testInput);
if(m.Success)
{
    string keywords = distRex.Replace(testInput, string.Empty);

    // you may want to further sanitize the keywords by replacing occurances of common wors
    //   like "and", "at", "within", "in", "is" etc.

    string distanceUnits = m.Groups["unit"].Value;
    int distance = Int32.Parse(m.Groups["dist"].Value);    
}

