简体   繁体   中英

Regex.Match throws FormatException

I am working with UDP and right now I am catching information sent out from a sensor. I am trying to extract only the numbers from the information send, which is Temperature, Light and Movement and trying to achieve that with Regex. But I am getting a FormatException that states:" Additional information: Input string was not in a correct format ". Here is the code:

UdpClient udpClient = new UdpClient(1337);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 0);

while (true)
{
    for (int i = 0; i < 5000; i++)
    {
        Byte[] receive = udpClient.Receive(ref endPoint);
        string receiveDat = Encoding.ASCII.GetString(receive);
        string result = Regex.Match(receiveDat, @"\D+").Value;

        Console.WriteLine(Int32.Parse(result)); 
        Console.ReadLine();
    }
}

You need to use a lowercase \\d in your regular expression if you want digits instead of non-digits. Capital \\D matches only non-digits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM