提取主机/端口组合使用.NET正则表达式 - 口部分可选组合、可选、端口、主机

2023-09-02 21:38:32 作者:素白

说我要提取的主机名和端口号的字符串是这样的:

Say I want to extract the hostname and the port number from a string like this:

stackoverflow.com:443

stackoverflow.com:443

这是pretty的方便。我可以做这样的事情:

That is pretty easy. I could do something like this:

(小于主机> *?):(?<港口> D *)

我不担心协议方案或有效的主机名/ IP地址或TCP / UDP端口,这并不重要,我的要求。

I am not worried about protocol schemes or valid host names/ip addresses or tcp/udp ports, it is not important to my request.

不过,我也需要支持一个扭曲的借此超出了我的常规前pressions知识 - 无需端口的主机名:

However, I also need to support one twist that takes this beyond my knowledge of regular expressions - the host name without the port:

stackoverflow.com

stackoverflow.com

我想用一个正则EX pression对于这一点,我想用命名的捕获组,使得主机组将始终存在于一个积极的比赛,而港口群存在,当且仅当我们有冒号后面跟一个数字的位数。

I want to use a single regular expression for this, and I want to use named capture groups such that the host group will always exist in a positive match, while the port group exists if and only if we have a colon followed by a number of digits.

我试图从它我软弱的理解做一个积极的后向:

I have tried doing a positive lookbehind from my feeble understanding of it:

(小于=?:)(<主机&GT *?)(?<港口> D *)

此接近,但冒号(:)被包括在主机捕获的结束。所以,我试图改变主机包括什么,但这样的冒号:

This comes close, but the colon (:) is included at the end of the host capture. So I tried to change the host to include anything but the colon like this:

(小于主机> [^:] *)(?< =:)(?<港口> D *)

这给了我一个空的主机捕获。

That gives me an empty host capture.

这是如何做到这一点,也就是使结肠癌和可选的端口号,但​​如果他们在那里,包括端口号捕获,使大肠消失有什么建议?

Any suggestions on how to accomplish this, i.e. make the colon and the port number optional, but if they are there, include the port number capture and make the colon "vanish"?

编辑:所有的四个答案我收到的工作很适合我,但要注意在一些人的意见。我接受了,因为不错的布局正则表达式结构和解释SLN的回答。感谢所有的答复!

All the four answers I have received work well for me, but pay attention to the comments in some of them. I accepted sln's answer because of the nice layout and explanation of the regexp structure. Thanks to all that replied!

推荐答案

这也许(小于主机> [^:] +)?(::(小于端口> D +) )?

 (?<host> [^:]+ )               # (1), Host, required
 (?:                            # Cluster group start, optional
      :                              # Colon ':'
      (?<port> d+ )                 # (2), Port number
 )?                             # Cluster group end

修改的 - 如果你不使用群集组,并使用捕获组作为群集组代替,这是何等的斑点网罪状的团体在其默认配置状态 -

edit - If you were to not use the cluster group, and use a capture group as that cluster group instead, this is how Dot-Net "counts" the groups in its default configuration state -

 (?<host> [^:]+ )         #_(2), Host, required                           
 (                        # (1 start), Unnamed capture group, optional
      :                        # Colon ':'
      (?<port> d+ )           #_(3), Port number                           
 )?                       # (1 end)
 
精彩推荐
图片推荐