How to validate pin code from address that address contain also a mobile number

280 Views Asked by At
1 :- ho no 55 A Akbarpur Behrampur vijay nagar 7827183008 Ghaziabad UP 201009
2 :- 47- A 57798/4343 ward no 7 ghaziabad 201009 UP 
3 :- 45456/343 ward 76587 Lucknow 110087
4 :- ho no 55 a behrampur ghaziabad 210 501

I have this type of string I have to validate only the pincode. The pincode can be anywhere within the address string. A 10 digit number should not be valid but 6 digit should be valid. The issue is that the address string may also contain a phone number.

public class PinCode
    {
        public void GetPinCode(string address)
        {
            var pincode = Regex.Match(address, @"[1-9][0-9]{2}[0-9]{3}");
            Console.WriteLine(pincode);
        }
    }
    class MainClass
    {
        public static void Main()
        {
            Console.Write("Enter the address : -");
            string input = Console.ReadLine();
            PinCode objPinCode = new PinCode();
            objPinCode.GetPinCode(input);
            Console.ReadLine();
        }
    }

I tried this user can enter any type of address

1

There are 1 best solutions below

0
On

You can with the regex \b([1-9][0-9]{5})\b
It will look for word boundary any digit 1..9 any digit 0..9 - exactly 5 matches word boundary

By using \b (word boundary), you'll ignore the longer (phone)numbers.

See this regex101 link to test the regex with your data.