Need help with Regex to extract zip code from string
c#, regex
Solution
This ought to do the trick:
`,\s(\d{4})`
And here is a brief example of how to use it:
using System;
using System.Text.RegularExpressions;
class Test
{
static void Main()
{
String input = "Sandviksveien 184, 1300 Sandvika";
Regex regex = new Regex(@",\s(\d{4})",
RegexOptions.Compiled |
RegexOptions.CultureInvariant);
Match match = regex.Match(input);
if (match.Success)
Console.WriteLine(match.Groups[1].Value);
}
}
Problem
I need to extract the zip code from a string. The string looks like this: ``` Sandviksveien 184, 1300 Sandvika ``` How can I use regex to extract the zip code? In the above string the zip code would be 1300. I've tried something along the road like this: ``` Regex pattern = new Regex(", [0..9]{4} "); string str = "Sandviksveien 184, 1300 Sandvika"; string[] substring = pattern.Split(str); lblMigrate.Text = substring[1].ToString(); ``` But this is not working.