Regular expression for UK postcode

I’m looking for an answer to my regular expresssion problem in c#. I’m looking for a match on a specific postcode format and have run into problems. Here you can find my solution with the regex pattern. C# public bool IsPostCode (string postcode) { return ( Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][A-HJKS-UWa-hjks-uw][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][A-Za-z][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z{2}$)”) || Regex.IsMatch(postcode, “(^[Gg][Ii][Rr][]*0[Aa][Aa]$)”) ); } Visual Basic Public Function IsPostCode(postcode As String) As Boolean Return (Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) OrElse Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][0-9][…

Read More

How to generate a random password

These code samples demonstrate how to generate a strong random password, which does not contain ambiguous characters (such as [1,l,I] or [O,0]). A strong password must be at least eight characters long and contain a combination of lower- and upper-case letters, numbers, and special characters. /////////////////////////////////////////////////////////////////////////////// // SAMPLE: Generates random password, which complies with the strong password // rules and does not contain ambiguous characters. // // To run this sample, create a new Visual C# project using the Console // Application template and replace the contents of the Class1.cs…

Read More