任何以模式开头的 url.. 执行此操作

发布于 2024-12-02 15:28:28 字数 1634 浏览 0 评论 0原文

我想将 url A (http://www.somehost.com/citizenship) 与模式 url B (http://www.somehost.com/*) 进行比较),如果 URL A 以模式 url B 开头,则执行此操作。由于 URL A 源自 URL B。所以任何以此模式开头的 url.. 只需在 if 循环中执行此操作...任何建议将不胜感激...!!

BufferedReader readbuffer = null;
        try {
            readbuffer = new BufferedReader(new FileReader("filters.txt"));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String strRead;


        try {
            while ((strRead=readbuffer.readLine())!=null){
                String splitarray[] = strRead.split(",");
                String firstentry = splitarray[0];
                String secondentry = splitarray[1];
                String thirdentry = splitarray[2];
                //String fourthentry = splitarray[3];
                //String fifthentry = splitarray[4];
                System.out.println(firstentry + " " + secondentry+ " " +thirdentry);
                URL url1 = new URL("http://www.somehost.com/citizenship");

 //Any url that starts with this pattern then do whatever you want in the if loop... How can we implement this??                    
                Pattern p = Pattern.compile("http://www.somehost.com/*");

                Matcher m = p.matcher(url1.toString());

                if (m.matches()) {
                  //Do whatever
                    System.out.println("Yes Done");
                }



                }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

I want to compare url A (http://www.somehost.com/citizenship) with the pattern url B (http://www.somehost.com/*) and if that URL A starts with the pattern url B then do this thing.. As URL A is originated from the URL B.. So any url that starts with this pattern.. just do this thing in the if loop... Any suggestions will be appreciated...!!

BufferedReader readbuffer = null;
        try {
            readbuffer = new BufferedReader(new FileReader("filters.txt"));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String strRead;


        try {
            while ((strRead=readbuffer.readLine())!=null){
                String splitarray[] = strRead.split(",");
                String firstentry = splitarray[0];
                String secondentry = splitarray[1];
                String thirdentry = splitarray[2];
                //String fourthentry = splitarray[3];
                //String fifthentry = splitarray[4];
                System.out.println(firstentry + " " + secondentry+ " " +thirdentry);
                URL url1 = new URL("http://www.somehost.com/citizenship");

 //Any url that starts with this pattern then do whatever you want in the if loop... How can we implement this??                    
                Pattern p = Pattern.compile("http://www.somehost.com/*");

                Matcher m = p.matcher(url1.toString());

                if (m.matches()) {
                  //Do whatever
                    System.out.println("Yes Done");
                }



                }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

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

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

发布评论

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

评论(2

云雾 2024-12-09 15:28:28

String.startsWith(String)

您可以像这样调用它:url1.toString().startsWith("http://www.somehost.com/")

或者我错过了你到底想要实现的目标吗?

无论如何,这是要构建的正则表达式: http://www\.somehost\.com/.*

您需要学习正则表达式语法,其中“.”表示任何字符,因此您需要在“www”和“somehost”之间对其进行转义。

在 Java 代码中,您还需要转义退格键,因此它变为: Pattern.compile("http://www\\.somehost\\.com/.*");

What's wrong with String.startsWith(String)?

You could invoke it like this: url1.toString().startsWith("http://www.somehost.com/").

Or did I miss what you're exactly trying to achieve?

Anyways, this is the regular expression to build: http://www\.somehost\.com/.*

You need to learn regular expression syntax, in which "." mean any character so you need to escape it in between "www" and "somehost".

In Java code, you need to escape backspaces as well hence it becomes: Pattern.compile("http://www\\.somehost\\.com/.*");

若言繁花未落 2024-12-09 15:28:28

我相信这应该可以解决问题:

    private final static String regexPattern = "http://www.somehost.com/[0-9a-zA-Z\\-]*/wireless-reach(/[0-9a-zA-Z\\-]*)*";
    String pattern = "http://www.somehost.com/citizen-ship/wireless-reach/fil1";

    if(pattern.matches(regex))
         System.out.println("Matched!");

I believe this should do the trick:

    private final static String regexPattern = "http://www.somehost.com/[0-9a-zA-Z\\-]*/wireless-reach(/[0-9a-zA-Z\\-]*)*";
    String pattern = "http://www.somehost.com/citizen-ship/wireless-reach/fil1";

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