简体   繁体   English

如何编写正则表达式以匹配ftp,ftps或sftp?

[英]How can I write a regular expression to match ftp, ftps or sftp?

I'm doing some URL validation and want to match http, https, ftp, ftps, sftp etc. 我正在做一些URL验证,想要匹配http,https,ftp,ftp,sftp等。

So far I have /(https?|ftps?)/ .. If I use s?ftps? 到目前为止,我有/(https?|ftps?)/ ..如果使用s?ftps? it'll match ftp, ftps, sftp and sftps. 它将匹配ftp,ftps,sftp sftps。

What's the best way to get around this? 什么是最好的解决方法? (sftps is not a valid URL scheme) (sftps不是有效的URL方案)

Try this: 尝试这个:

/(ht|f)tps?|sftp/

Should work. 应该管用。

这应该覆盖您的所有基地...

/(ht|f)tps?|sftp/

You can use: 您可以使用:

/^(?:ht|f)tps?|sftp/

or 要么

/^(?!sftps)(?:https?|s?ftps?)/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写正则表达式来匹配不以短语结尾的行? - How can I write a regular expression to match lines not ending with a phrase? 如何用正则表达式匹配所有内容? - How can I match everything with a regular expression? 如何编写正则表达式以匹配自由文本和空格? - How to can I write a regular expression to match free-text followed by whitespace? 如何将第二次出现的字符与正则表达式匹配? - How can I match the second occurrence of a character with a regular expression? 如何显示不匹配正则表达式的字符列表? - How can I display a list of characters that fail to match a regular expression? 如何在Perl中进行全局正则表达式匹配? - How can I do a global regular expression match in Perl? 如何编写仅在匹配三个必需的捕获组时才匹配的正则表达式 - How do I write a regular expression that only matches if match three required capture groups 如何编写正则表达式来匹配免费电子邮件帐户? - How to write regular expression to match free email accounts? 如何在 PHP 中使用与整个字符串中的给定组匹配的正则表达式,而不是在第一次匹配时停止 - How can i use a regular expression in PHP that matches a given group in the whole string instead of stops at first match 如何使用PHP在sftp服务器上写入文件? - How can I write a file on an sftp-server with PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM