首页 > 解决方案 > 在正则表达式中使一些信息可选

问题描述

我正在为以下模式编写正则表达式:sftp://user:password@host[:port]/path

我写了以下sftp://(.+):(.+)@(.+):(\d+)/(.*)与模式匹配的内容,其中group1匹配usergroup2匹配passwordgroup3匹配host namegroup4匹配port numbergroup5匹配path

但是,端口号可以是可选参数,我尝试了下面的正则表达式,其中端口组后跟一个?.

sftp://(.+):(.+)@(.+)(:(\d+))?\/(.*)

这里group3host:portwhich 不是预期的匹配。

如何制作端口参数可选的正则表达式?

标签: regex

解决方案


利用

sftp://([^/@]+):([^/@]+)@([^/]+?)(?::(\d+))?/(.*)

证明

解释

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  sftp://                  'sftp://'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^/@]+                   any character except: '/', '@' (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  :                        ':'
--------------------------------------------------------------------------------
  (                        group and capture to \2:
--------------------------------------------------------------------------------
    [^/@]+                   any character except: '/', '@' (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \2
--------------------------------------------------------------------------------
  @                        '@'
--------------------------------------------------------------------------------
  (                        group and capture to \3:
--------------------------------------------------------------------------------
    [^/]+?                   any character except: '/' (1 or more
                             times (matching the least amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \3
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
    (                        group and capture to \4:
--------------------------------------------------------------------------------
      \d+                      digits (0-9) (1 or more times
                               (matching the most amount possible))
--------------------------------------------------------------------------------
    )                        end of \4
--------------------------------------------------------------------------------
  )?                       end of grouping
--------------------------------------------------------------------------------
  /                        '/'
--------------------------------------------------------------------------------
  (                        group and capture to \5:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of \5

推荐阅读