English 中文(简体)
如何用 Regex 获得这个 URL 参数
原标题:How can I get this URL parameter with regex
  • 时间:2012-05-23 21:26:17
  •  标签:
  • java
  • regex
String url = "mysite.com/index.php?name=john&id=432"

如何提取 ID 参数 (432)?

它可以在URL的任何位置上, ID的长度也不同

最佳回答

http://hc.apache.org/http://hc.ahrf="http://hc.apache.org/httpsublicents-client-ga/httpclient/apidocs/org/pache/http/client/utils/URLEncoddUtils.html" rel=“nreferrer”>URLEncodUtils HtpClient 包:

import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import java.nio.charset.Charset;
import java.util.List;

public class UrlParsing {
    public static void main(String[] a){
        String url="http://mysite.com/index.php?name=john&id=42";
        List<NameValuePair> args= URLEncodedUtils.parse(url, Charset.defaultCharset());
        for (NameValuePair arg:args)
            if (arg.getName().equals("id"))
                System.out.println(arg.getValue());
    }
}

此打印 42 到控制台 。

If you have the url stored in a URI object, you may find useful an overload of URLEncodedUtils.parse that accept directly an URI instance. If you use this overloaded version, you have to give the charset as a string:

URI uri = URI.create("http://mysite.com/index.php?name=john&id=42");
List<NameValuePair> args= URLEncodedUtils.parse(uri, "UTF-8");
问题回答

我只是给一个抽象的regex。 在 之后添加您不希望在 id 中出现的任何内容 [\\ amp;

Pattern pattern = Pattern.compile("id=([^&]*?)$|id=([^&]*?)&");

Matcher matcher = pattern.matcher(url);

if (matcher.matches()) {
    int idg1   = Integer.parseInt(matcher.group(1));
    int idg2   = Integer.parseInt(matcher.group(2));
}

idg1 idg2 都有价值。

您可以使用:

String id = url.replaceAll("^.*?(?:\?|&)id=(\d+)(?:&|$).*$", "$1");

regex已经给出了, 但你也可以通过一些简单的分裂来做到这一点:

public static String getId(String url) {
        String[] params = url.split("\?");
        if(params.length==2) {
                String[] keyValuePairs = params[1].split("&");
                for(String kvp : keyValuePairs) {
                        String[] kv = kvp.split("=");
                        if(kv[0].equals("id")) {
                                return kv[1];
                        }
                }
        }
        throw new IllegalStateException("id not found");
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签