2

Hi am looking for an Regular Expression which parses QueryString.Am using the below regex:

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

But its throwing

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

Can anyone help me on

4
  • Regex used is"[\\?&](?<name>[^&=]+)=(\\?<value>[^&=]+)" Commented Feb 14, 2012 at 16:12
  • Can someone help me its urgent Commented Feb 14, 2012 at 16:12
  • 1
    Are you sure that you're using Java 7 / JDK 1.7? Because it looks like the regex-engine isn't recognizing the named capture-group notation. Commented Feb 14, 2012 at 16:15
  • doing this without a regex is (I think) a lot more clear and straightforward. Commented Feb 14, 2012 at 16:26

3 Answers 3

6

Java <1.7 doesn't support named groups so change your regex to:

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

and then from the Matcher, get group # 1 and # 2 for the name and value.

Update:

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]
Sign up to request clarification or add additional context in comments.

2 Comments

StringBuffer str1=new StringBuffer("test.abc.com/test/http/com/google/www/:/…);
My url goes like the above one.can u help me.am getting an error as match not found
0

I don't know what you are trying to accomplish with "%5b^&=%5d+">. Sounds like it's complaining about the >. This can help: http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/

Comments

0

Lookbehind expressions must look for a fixed length string. You cannot use regex features such as * ? + inside lookbehind assertions. (?<name>[^&=]+) is a lookbehind denoted by '?<' and it contains a '+'. Maybe you wanted to comment out the '<'?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.