Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resolved Inventory Regex issues

Joined
Dec 10, 2014
Messages
3,255
Found a weird issue where my Inventory.contains(Pattern) was returning false for a certain item. It's worked fine for everything else and this is the first time I've encountered this behavior.
Code:
Code:
this.name = name; 
this.pattern = Regex.getPatternForExactString(name); 
if(Inventory.contains(name)){
    System.out.println("Contains name:"+name); 
    if(Inventory.contains(pattern)){
        System.out.println("Contains pattern:"+pattern);
    } else {
        System.out.println("Doesn't contain pattern:" + pattern);
    }
} else {
    System.out.println("Doesn't contain name:"+name);
}
Output:
Code:
(07:47:18) Contains name:Weapon poison++ (unf)
(07:47:18) Doesn't contain pattern:^Weapon poison++ \(unf\)$

Inventory.contains(pattern) is simply !Inventory.newQuery().names(pattern).results().isEmpty();

If I use getPatternForContainsString(name) it returns true though.

@Cloud
 
Engineer
Joined
Jul 28, 2013
Messages
2,776
That points to "^Weapon poison++ \(unf\)$" being the incorrect regex for it. What should the regex for it look like?
 
Joined
Dec 10, 2014
Messages
3,255
That points to "^Weapon poison++ \(unf\)$" being the incorrect regex for it. What should the regex for it look like?
From the top of my head: Pattern.compile("^Weapon poison++ \\(unf\\)"); should work, but I'm pretty sure that's what's being used already as the output matches what it should be.

No wait, maybe the + needs to be escaped?
 
Engineer
Joined
Jul 28, 2013
Messages
2,776
From the top of my head: Pattern.compile("^Weapon poison++ \\(unf\\)"); should work, but I'm pretty sure that's what's being used already as the output matches what it should be.

No wait, maybe the + needs to be escaped?
Play with it for me until you get it working :p
Code:
public static Pattern getPatternForExactString(final String string) {
if (string == null) {
return Pattern.compile("");
}
String pattern = "^";
for (char c : string.toCharArray()) {
if (c == '(' || c == ')' || c == '^' || c == '$'|| c == '.' || c == '*' || c == '?' || c == '|'|| c == '[' || c == '{') {
pattern += "\\";
}
pattern += c;
}
pattern += "$";
return Pattern.compile(pattern);
}
 
Joined
Dec 10, 2014
Messages
3,255
Play with it for me until you get it working :p
Code:
public static Pattern getPatternForExactString(final String string) {
if (string == null) {
return Pattern.compile("");
}
String pattern = "^";
for (char c : string.toCharArray()) {
if (c == '(' || c == ')' || c == '^' || c == '$'|| c == '.' || c == '*' || c == '?' || c == '|'|| c == '[' || c == '{') {
pattern += "\\";
}
pattern += c;
}
pattern += "$";
return Pattern.compile(pattern);
}
Yeah you need to escape the '+'s, so add that to the if c == <escapedchar> line and it should be all g :)
 
Joined
Dec 10, 2014
Messages
3,255
Also, I just confirmed in game that
Pattern.compile("Weapon poison\\+\\+ \\(unf\\)")
is the correct regex for
"Weapon poison++ (unf)"
 
Top