特定 url 格式的正则表达式

发布于 2024-11-18 05:47:51 字数 513 浏览 2 评论 0原文

我正在尝试获取正则表达式来匹配特定的 url 格式。特别是 stackexchange 的 api url。例如,我希望这两者匹配:

http://api.stackoverflow.com/1.1/questions/1234/answers  
http://api.physics.stackexchange.com/1.0/questions/5678/answers

  • 非粗体的所有内容都必须相同。
  • 第一个粗体部分只能由 a 到 z 组成,可以有一个句号,也可以没有句号。
    • 另外,如果有一个句号,后面必须跟有“stackexchange”一词,那就太好了。但这并不重要。
  • 第二个粗体部分只能是 1 或 0。
  • 最后一个粗体部分只能是数字 0 到 9,并且可以是
  • 任意长度 url 之前或之后不能有任何内容,甚至不能有尾部斜杠

I am trying to get a regex expression to match a specific url format. Specifically the api urls for stackexchange. For example I want both of these to match:

http://api.stackoverflow.com/1.1/questions/1234/answers  
http://api.physics.stackexchange.com/1.0/questions/5678/answers

Where

  • everything not in bold must identical.
  • The first bold part, can only be made of a to z, and either one or no full stop.
    • Also it would be good, if there is one full stop the word "stackexchange" must follow. However this isn't crucial.
  • The second bold part can only be a 1 or a 0.
  • The last bold part can be only numbers 0 to 9, and can be any length
  • There can't be anything at all before or after the url, not even a trailing slash

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

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

发布评论

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

评论(3

花开半夏魅人心 2024-11-25 05:47:51
Pattern.compile("^(?i:http://api\\.(?:[a-z]+(?:\\.stackexchange)?)\\.com)/1\\.[01]/questions/[0-9]+/answers\\z")

^ 确保它在输入开始时开始,\\z 确保它在输入结束时结束。所有的点都被转义了,所以它们都是字面意思。根据 URL 规范,(?i:...) 部分使域和方案不区分大小写。 [01] 仅匹配字符 0 或 1。[0-9]+ 匹配 1 个或多个阿拉伯数字。其余的都是不言自明的。

Pattern.compile("^(?i:http://api\\.(?:[a-z]+(?:\\.stackexchange)?)\\.com)/1\\.[01]/questions/[0-9]+/answers\\z")

The ^ makes sure it starts at the start of input, and the \\z makes sure it ends at the end of input. All the dots are escaped so they are literal. The (?i:...) part makes the domain and scheme case-insensitive as per the URL spec. The [01] only matches the characters 0 or 1. The [0-9]+ matches 1 or more Arabic digits. The rest is self explanatory.

书信已泛黄 2024-11-25 05:47:51
^http://api[.][a-z]+([.]stackexchange)?[.]com/1[.][01]/questions/[0-9]+/answers$

^ 匹配字符串开头,$ 匹配行尾,[.] 是转义点的另一种方法,而不是反斜杠(其本身需要转义为 \\.)。

^http://api[.][a-z]+([.]stackexchange)?[.]com/1[.][01]/questions/[0-9]+/answers$

^ matches start-of-string, $ matches end-of-line, [.] is an alternative way to escape the dot than a backslash (which itself would need to be escaped as \\.).

苏别ゝ 2024-11-25 05:47:51

这个经过测试的 Java 程序有一个带注释的正则表达式,应该可以解决这个问题:

import java.util.regex.*;
public class TEST {
    public static void main(String[] args) {
        String s = "http://api.stackoverflow.com/1.1/questions/1234/answers";

        Pattern p = Pattern.compile(
            "http://api\\.              # Scheme and api subdomain.\n" +
            "(?:                        # Group for domain alternatives.\n" +
            "  stackoverflow            # Either one\n" +
            "| physics\\.stackexchange  # or the other\n" +
            ")                          # End group for domain alternatives.\n" +
            "\\.com                     # TLD\n" +
            "/1\\.[01]                  # Either 1.0 or 1.1\n" +
            "/questions/\\d+/answers    # Rest of path.", 
            Pattern.COMMENTS);
        Matcher m = p.matcher(s);
        if (m.matches()) {
            System.out.print("Match found.\n");
        } else {
            System.out.print("No match found.\n");
        }
    }
}

This tested Java program has a commented regex which should do the trick:

import java.util.regex.*;
public class TEST {
    public static void main(String[] args) {
        String s = "http://api.stackoverflow.com/1.1/questions/1234/answers";

        Pattern p = Pattern.compile(
            "http://api\\.              # Scheme and api subdomain.\n" +
            "(?:                        # Group for domain alternatives.\n" +
            "  stackoverflow            # Either one\n" +
            "| physics\\.stackexchange  # or the other\n" +
            ")                          # End group for domain alternatives.\n" +
            "\\.com                     # TLD\n" +
            "/1\\.[01]                  # Either 1.0 or 1.1\n" +
            "/questions/\\d+/answers    # Rest of path.", 
            Pattern.COMMENTS);
        Matcher m = p.matcher(s);
        if (m.matches()) {
            System.out.print("Match found.\n");
        } else {
            System.out.print("No match found.\n");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文