如何使用 。在re.match()中

I want to make a basic chat program using socket and I want to ask client the address. The address format is IP port. IP is like num.num.num.num and port is just a 4 or 5 digit number. I want to use re.match function to check if inpu is a valid address. How can I use . as character?

评论
  • ramet
    ramet 回复

    You escape things with \ so you search for \..

    There is a bit more to checking IP addresses though. so there are multiple options depending on how exact vs complicated you want your regex to be. This site has more information.

  • 命*
    命* 回复

    To use . as a character in regular expressions, escape it with a backslash like this: \.

    因此,要匹配IP端口地址,可以使用:

    [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*