Reputation: 45
I have a filter param in my REST API that can look like this:
owner='bob' // owner is bob
owner!'bob' // owner is not bob
owner.town='bel*' // owner's town starts with bel
owner.town='*bel*' // owner's town contains bel
owner.car='{ some json blob here}' // owner's car equals some json blob
owner.car~ // owner has a property named car
So I want to capture:
I have started with the following, buts it's not returning what I'd expect:
String filter = "owner='bob'";
Pattern.compile("(\\w+?)(=|!|~)(*?)(\\w+?)(*?)");
final Matcher matcher = pattern.matcher(filter);
// Results
matcher.group(1) // "owner"
matcher.group(2) // "="
matcher.group(3) // "\"
matcher.group(4) // ""
matcher.group(5) // "bob"
matcher.group(6) // ""
matcher.group(7) // "\"
Problems I'm having:
(*?)
is correctly capturing zero or one asterisksmatcher.group(anything)
I'm pretty sure there are other problems with the regex I havn't found yet...
Upvotes: 0
Views: 379
Reputation: 24812
I would use the following regex :
^([\w.:-]+)([=!~])('.*')?$
It is composed of three consecutive groups defined as follows :
You can try it here.
Note that if the mention that the value can start or end with a *
is supposed to imply that it can't contain *
in other places you will want to change the third group into ('\*?[^*]*\*?')
.
Upvotes: 1