regex - 在正则表达式中使一些信息可选
问题描述
我正在为以下模式编写正则表达式:sftp://user:password@host[:port]/path
我写了以下sftp://(.+):(.+)@(.+):(\d+)/(.*)
与模式匹配的内容,其中group1
匹配user
、group2
匹配password
、group3
匹配host name
和group4
匹配port number
和group5
匹配path
但是,端口号可以是可选参数,我尝试了下面的正则表达式,其中端口组后跟一个?
.
sftp://(.+):(.+)@(.+)(:(\d+))?\/(.*)
这里group3
与host:port
which 不是预期的匹配。
如何制作端口参数可选的正则表达式?
解决方案
利用
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
推荐阅读
- matlab - Matlab中的图像改组逆误差
- sql - 根据当前 PK 计算删除规则操作中的新值
- javascript - 使用 trackBy 时更新子组件
- javascript - Javascript 显示alert() 之前和之后的样式
- matlab - 在 MATLAB 中使用 set(gca,'LineWidth', x) 创建一个新图形,而原始图像具有默认宽度
- java - Java程序打印字符串中的第一个非重复字符
- c# - 如何在c#中更新库存数量?
- android - 调整大小并保存图像
- android - 想要使用 mongodb 作为数据库创建 android 应用程序登录和注册 servlet
- android - 如何从 JSONobject 中的嵌套 JSONarray 收集数据?