0

I am implementing RESTful query with JPA specification. Would like to handle url with multiple criteria that looks like this:

http://localhost:8080/samples?search=lastName:doe,age>25

The search string would match pattern (\\w+?)(:|<|>)(\\w+?) separated by ",".

Therefore, I wrote the following code to obtain Matcher from a string:

static Matcher getMatcherFromString(String str) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
    Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
    Matcher matcher = pattern.matcher(str + ",");
    return matcher;
}

This method is then called in controller to parse the url.

However, when I tested the method with string analysisId:fdebfd6e-d046-4192-8b97-ac9f65dc2009, it returns null. Why did I do wrong for the pattern matching?

5
  • I'd say because \w does not match - Commented Dec 20, 2016 at 16:39
  • @Fallenhero \w is for words. What should I use instead? Commented Dec 20, 2016 at 16:40
  • I don't know what you like to match, but maybe [0-9a-zA-Z_-]? Commented Dec 20, 2016 at 16:41
  • or easier (if supported) [\w-] Commented Dec 20, 2016 at 16:42
  • @Fallenhero tried both ([a-zA-Z_-])(:|<|>)([0-9a-zA-Z_-]) and ([\w-])(:|<|>)([\w-]) and still does not work. I think the individual pattern is fine. It is the matcher groups that is not working. Commented Dec 20, 2016 at 17:05

2 Answers 2

2

I got this to work: (\w+?)(:|<|>)[a-zA-Z0-9\-]*,

I used this to figure it out: https://regex101.com/r/fOEzd9/1

I think the main problem was the numbers makes \w not match as a word. You also need to account for the dashes.

Sign up to request clarification or add additional context in comments.

Comments

1

I had the same problem and I solved using this string:

"(\\w+?)(:|<|>)(\\w+?),"

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.