【问题标题】:Pattern matching of a url pathurl路径的模式匹配
【发布时间】:2018-04-12 20:46:38
【问题描述】:

我有以下需要解析的网址:

a) https://url:port/abc
b) https://url:port/abc/{uid}
c) https://url:port/abc/{uid}/def

为了弄清楚 url 的类型(a、b 或 c),我正在做:

Pattern a = Pattern.compile(".*\\/abc$");
Pattern b = ??
Pattern c = Pattern.compile(".*\\/abc\\/(.*?)\\/def$");

模式 ac 工作正常。虽然我不确定我可以使用什么模式来匹配 b 类型的确切 url,而不必依赖匹配的顺序。

【问题讨论】:

    标签: java regex split pattern-matching


    【解决方案1】:

    你可以用这个:

     .*\\/abc\\/([^\\/]*)$
    

    Demo

    也不是斜线后面的部分:([^\\/]*)$ 它允许任意数量的非斜线字符,因此它允许 uid 但不允许路径的其他部分。

    【讨论】:

    • 啊,这就是我要找的东西!
    • 顺便说一句,斜线是什么意思?
    • "not slashes" - 奇怪的错字:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    • 2022-08-11
    • 2014-02-21
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 2016-12-11
    相关资源
    最近更新 更多