Question

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??

Was it helpful?

Solution 2

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();
     }
 }

OTHER TIPS

(Somewhat speculative, but hopefully helpful.)

If "constant part of the string" contains characters which have special meaning in a regular expression, that could easily be a problem. The way to solve this is to use Pattern.quote. For example:

// ^ to anchor the match to the start of the line/string. May not be required.
Pattern pattern = Pattern.compile("^" + Pattern.quote(expectedPrefix) + ".*");

Of course I'd only use this if I had to use regular expressions. Otherwise I'd just use:

if (actualText.startsWith(expectedText))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top