2012-02-14 54 views
2

嗨正在尋找其解析使用QueryString.Am下面的正則表達式的正則表達式:正則表達式解析的queryString

Pattern pr1=Pattern.compile("[\\?&](?<name>[^&=]+)=(?<value>[^&=]+)"); 

但它扔

java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 18 
[\?&](?<name>[^&=]+)=(?<value>[^&=]+) 
       ^

誰能幫我

+0

正則表達式是 「[\\?&](? [^&=] +)=(\\? [^&=] +)」 – rkhm 2012-02-14 16:12:17

+0

有人可以幫助我的迫切 – rkhm 2012-02-14 16:12:44

+1

你確定你是否使用Java 7/JDK 1.7?因爲它看起來像正則表達式引擎不識別命名的捕獲組表示法。 – ruakh 2012-02-14 16:15:14

回答

5

的Java < 1.7不支持命名的組,以改變你的正則表達式:

Pattern pr1 = Pattern.compile("[\\?&]([^&=]+)=([^&=]+)"); 

,然後從匹配器,獲取組#1和#2的名稱和值。

更新:

String str = 
     "http://test.abc.com/test/http/com/google/www/:/?code=1234&Id=123354455656%22"; 
Pattern pt = Pattern.compile("[\\?&]([^&=]+)=([^&=]+)"); 
Matcher m = pt.matcher(str); 
if (m.find()) 
    System.out.printf("name=[%s], value=[%s]%n", m.group(1), m.group(2)); 

OUTPUT:

name=[code], value=[1234] 
+0

沒有這個不起作用 – rkhm 2012-02-14 16:30:35

+0

StringBuffer str1 = new StringBuffer(「http://test.abc.com/test/http/com/google/www/:/?code=1234&Id=123354455656」) ; – rkhm 2012-02-14 16:32:13

+0

我的網址就像上面的那樣。可以幫助我.am因爲找不到匹配而出錯。 – rkhm 2012-02-14 16:32:53

0

回顧後表達式必須尋找一個固定長度的字符串。你不能使用正則表達式功能,如*? +裏面的lookbehind斷言。 (?<name>[^&=]+)是用'?'表示的倒影。 <',它包含一個'+'。也許你想評論'<'?使用