javaregexswtbot

SWTBotTree expand node by regular expression


I'd like to use SWTBotTree.expandNode (final String nodeText, final boolean recursive) function with a String that is not always exactly the same. The beginning of the string is the same but the end can be varying. I thought it can be solved with reg. exp. something like: "constant part of the string"+".*" but it was not recognized as a reg. exp. What should I do??


Solution

  • I am not sure exactly how you are trying to do this, but if you are doing:

    swtBotTree.expandNode("constant part of string" + ".*", true);
    

    Then you are most likely doing it wrong...

    The javadoc for expandNode gives no mention that it supports regular expressions...

    Looking into the source (just some version I found when searching), expandNode uses another method called getItem(...). That method does something like if (item.getText().equals(nodeText)). In other words, that method uses string equality comparison, not regex matching.

    In Java, there is support for regular expressions (take a look at Pattern javadoc and Matcher javadoc or on String.matches(...) javadoc), but only if that functionality is used in the implementation. You cannot pass a regex to a method which expects a normal String and expect it to do regex matching.

    Short answer:

    Java has classes which provide regex functionality, but the SWTBotTree.expandNode(String, boolean) method does not support regex.

    EDIT:

    As far as I understand, there is no direct support in SWTBotTree for doing regex/wildcard lookups.

    If you desperately want to do this, you could try something like:

     for(SWTBotTreeItem item : swtBotTree.getAllItems()){
         if(item.getText().matches("some regex goes here")){
             item.expand();
         }
     }