要检查的正则表达式以 http://、https:// 或 ftp:// 开头

发布于 2024-12-14 13:13:53 字数 625 浏览 1 评论 0原文

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

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

    }
}

它打印false。我还检查了 stackoverflow 帖子 正则表达式来测试字符串是否以以下开头http:// 或 https://

正则表达式似乎是正确的,但为什么它不匹配?我什至尝试了 ^(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{

    }
}

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鹿童谣 2024-12-21 13:13:53

您需要在此处进行整个输入匹配。

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

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

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

You need a whole input match here.

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

Edit:(Based on @davidchambers's comment)

System.out.println(test.matches("^(https?|ftp)://.*$")); 
音栖息无 2024-12-21 13:13:53

除非有一些令人信服的理由使用正则表达式,否则我只会使用 String.startsWith:

bool matches = test.startsWith("http://")
            || test.startsWith("https://") 
            || test.startsWith("ftp://");

如果这也更快,我不会感到惊讶。

Unless there is some compelling reason to use a regex, I would just use String.startsWith:

bool matches = test.startsWith("http://")
            || test.startsWith("https://") 
            || test.startsWith("ftp://");

I wouldn't be surprised if this is faster, too.

撩动你心 2024-12-21 13:13:53

如果您想以不区分大小写的方式执行此操作,则更好:

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

If you wanna do it in case-insensitive way, this is better:

System.out.println(test.matches("^(?i)(https?|ftp)://.*$")); 
南…巷孤猫 2024-12-21 13:13:53

我认为正则表达式/字符串解析解决方案很棒,但对于这个特定的上下文,似乎仅使用 java 的 url 解析器就有意义:

https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

取自该页面:

import java.net.*;
import java.io.*;

public class ParseURL {
    public static void main(String[] args) throws Exception {

        URL aURL = new URL("http://example.com:80/docs/books/tutorial"
                           + "/index.html?name=networking#DOWNLOADING");

        System.out.println("protocol = " + aURL.getProtocol());
        System.out.println("authority = " + aURL.getAuthority());
        System.out.println("host = " + aURL.getHost());
        System.out.println("port = " + aURL.getPort());
        System.out.println("path = " + aURL.getPath());
        System.out.println("query = " + aURL.getQuery());
        System.out.println("filename = " + aURL.getFile());
        System.out.println("ref = " + aURL.getRef());
    }
}

产量 下列:

protocol = http
authority = example.com:80
host = example.com
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING

I think the regex / string parsing solutions are great, but for this particular context, it seems like it would make sense just to use java's url parser:

https://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

Taken from that page:

import java.net.*;
import java.io.*;

public class ParseURL {
    public static void main(String[] args) throws Exception {

        URL aURL = new URL("http://example.com:80/docs/books/tutorial"
                           + "/index.html?name=networking#DOWNLOADING");

        System.out.println("protocol = " + aURL.getProtocol());
        System.out.println("authority = " + aURL.getAuthority());
        System.out.println("host = " + aURL.getHost());
        System.out.println("port = " + aURL.getPort());
        System.out.println("path = " + aURL.getPath());
        System.out.println("query = " + aURL.getQuery());
        System.out.println("filename = " + aURL.getFile());
        System.out.println("ref = " + aURL.getRef());
    }
}

yields the following:

protocol = http
authority = example.com:80
host = example.com
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING
清风无影 2024-12-21 13:13:53

test.matches() 方法检查所有文本。use test.find()

test.matches() method checks all text.use test.find()

予囚 2024-12-21 13:13:53

在startsWith和matches之间添加验证。

import java.net.URL


fun main(args: Array<String>) {
    
    val IMAGE_SERVER = "https://google.com/file/"
    val IMAGE_REG: String by lazy {
        val url = URL(IMAGE_SERVER)
        "^(http|https)://${url.host}${url.path}.*\$"
    }
    
    val regx = Regex(IMAGE_REG)
    println(IMAGE_REG)
    

    var begin = System.nanoTime()
    var aa = IMAGE_SERVER.startsWith(IMAGE_SERVER)
    println("startsWith:"+ (System.nanoTime()-begin))
    println("startsWith:"+ aa)
    
    begin = System.nanoTime()
    aa = IMAGE_SERVER.matches(regx)
    println("matches:"+ (System.nanoTime()-begin))
    println("matches:"+ aa)
    

}

开始于:3755625
开始于:true
匹配:174250
matches:true

matches 为 174us,startswith 为 3.755ms

matches 在场景中的性能和代码整洁度上比startsWith 好得多。

Add a verification between startsWith and matches.

import java.net.URL


fun main(args: Array<String>) {
    
    val IMAGE_SERVER = "https://google.com/file/"
    val IMAGE_REG: String by lazy {
        val url = URL(IMAGE_SERVER)
        "^(http|https)://${url.host}${url.path}.*\
quot;
    }
    
    val regx = Regex(IMAGE_REG)
    println(IMAGE_REG)
    

    var begin = System.nanoTime()
    var aa = IMAGE_SERVER.startsWith(IMAGE_SERVER)
    println("startsWith:"+ (System.nanoTime()-begin))
    println("startsWith:"+ aa)
    
    begin = System.nanoTime()
    aa = IMAGE_SERVER.matches(regx)
    println("matches:"+ (System.nanoTime()-begin))
    println("matches:"+ aa)
    

}

startsWith:3755625
startsWith:true
matches:174250
matches:true

matches is 174us, startswith is 3.755ms

matches is much better than startsWith on performance and code cleanness in the scenario.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文