正则表达式以http://,https://或ftp://开头 [英] Regex to check with starts with http://, https:// or ftp://

查看:1035
本文介绍了正则表达式以http://,https://或ftp://开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个正则表达式,以检查单词是否以http://https://ftp://开头,我的代码如下,

I am framing a regex to check if a word starts with http:// or https:// or ftp://, my code is as follows,

     public static void main(String[] args) {
    try{
        String test = "http://yahoo.com";
        System.out.println(test.matches("^(http|https|ftp)://"));
    } finally{

    }
}

它打印false.我还检查了stackoverflow帖子 Regex,以测试字符串是否以http://或https://

It prints false. I also checked stackoverflow post Regex to test if string begins with http:// or https://

正则表达式似乎正确,但是为什么不匹配?我什至尝试了^(http|https|ftp)\://^(http|https|ftp)\\://

The regex seems to be right but why is it not matching?. I even tried ^(http|https|ftp)\:// and ^(http|https|ftp)\\://

推荐答案

您需要在此处输入整个输入匹配项.

You need a whole input match here.

System.out.println(test.matches("^(http|https|ftp)://.*$")); 

编辑 :(基于 @davidchambers 的评论)

System.out.println(test.matches("^(https?|ftp)://.*$")); 

这篇关于正则表达式以http://,https://或ftp://开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆