1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Resolved Inventory Regex issues

Discussion in 'Client & Site Support' started by SlashnHax, Aug 5, 2015.

  1. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    3,198
    Likes Received:
    1,041
    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 (Text):
    1. this.name = name;
    2. this.pattern = Regex.getPatternForExactString(name);
    3. if(Inventory.contains(name)){
    4.     System.out.println("Contains name:"+name);
    5.     if(Inventory.contains(pattern)){
    6.         System.out.println("Contains pattern:"+pattern);
    7.     } else {
    8.         System.out.println("Doesn't contain pattern:" + pattern);
    9.     }
    10. } else {
    11.     System.out.println("Doesn't contain name:"+name);
    12. }
    Output:
    Code (Text):
    1. (07:47:18) Contains name:Weapon poison++ (unf)
    2. (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

     
  2. Best Answer:
    Post #8 by Cloud, Aug 7, 2015
  3. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    That points to "^Weapon poison++ \(unf\)$" being the incorrect regex for it. What should the regex for it look like?
     
  4. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    3,198
    Likes Received:
    1,041
    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?
     
  5. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    Play with it for me until you get it working :p
    Code (Text):
    1.  
    2. public static Pattern getPatternForExactString(final String string) {
    3. if (string == null) {
    4. return Pattern.compile("");
    5. }
    6. String pattern = "^";
    7. for (char c : string.toCharArray()) {
    8. if (c == '(' || c == ')' || c == '^' || c == '$'|| c == '.' || c == '*' || c == '?' || c == '|'|| c == '[' || c == '{') {
    9. pattern += "\\";
    10. }
    11. pattern += c;
    12. }
    13. pattern += "$";
    14. return Pattern.compile(pattern);
    15. }
    16.  
     
  6. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    3,198
    Likes Received:
    1,041
    Yeah you need to escape the '+'s, so add that to the if c == <escapedchar> line and it should be all g :)
     
  7. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    Did you test to verify that?
     
  8. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    3,198
    Likes Received:
    1,041
    I tested using IntelliJ's regex tester, as well as the online tester I used to use before I discovered IntelliJ's inbuilt one
     
  9. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    Alright adjusted for the next release.
     
    SlashnHax likes this.
  10. SlashnHax

    Joined:
    Dec 10, 2014
    Messages:
    3,198
    Likes Received:
    1,041
    Also, I just confirmed in game that
    Pattern.compile("Weapon poison\\+\\+ \\(unf\\)")
    is the correct regex for
    "Weapon poison++ (unf)"
     

Share This Page

Loading...