Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Matching using Regex

$
0
0

Hi everyone,

I am getting trouble with matching a number of words.

For example, If 

input =@"from New York to Los Angeles in a car at 55 mph";

"i want to match A = New York, B = Los Angeles, C = a car, D = 55 mph

if input is "to Los Angeles in a car at 55 mph", i expected to get (A: null, B: Los Angeles, C: a car, D: 55 mph)

or if input is "from New York in a car at 55 mph", i exptect to get (A: NewYork, B: null, C: a car, D: 55 mph)

I try this one, but it does not work

string input =@"from New York to Los Angeles in a car at 55 mph";string pattern = @"(from)*\s*(.+)*\s*(to)*\s*(.+)\s*(in)*\s*(.+)*\s*(at)\s*(.+)*$";Match match =Regex.Match( input, pattern );if( match.Success){string A = match.Groups[1].Value;string B = match.Groups[2].Value;string C = match.Groups[3].Value;string D = match.Groups[4].Value;Console.WriteLine("\"{0}\", \"{1}\", \"{2}\", \"{3}\"", A, B, C, D );}

Any idea on this problem. Hope to hear from you.

Best regards

Vu The Huynh


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>